Derived classes in C - What is your favorite method?

后端 未结 9 1255
既然无缘
既然无缘 2020-12-17 04:10

In my experience in object oriented C programming I have seen two ways to implement derived classes.


First Method, have a definition of the pa

9条回答
  •  孤城傲影
    2020-12-17 04:26

    The code I worked with used the first method.

    The only two reasons I can think of for using the first method is:

    1. Saves you some cycles because you will be doing one less de-referencing
    2. When you cast the derived class pointer to the parent class, i.e. (struct parent_class *)ptr_my_derived_class , you know 100% what the expected result will be.

    I prefer the first method because you can cast the derived class pointer to the parent class without any worries.

    Can you do the same thing with the second method? ( It seems like you would have to jump through a hoop or two to get the same end result )

    If you can do the same with method 2, then I think both methods would be equal.

提交回复
热议问题