Derived classes in C - What is your favorite method?

后端 未结 9 1229
既然无缘
既然无缘 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:41

    The second way has the advantage of typesafety with inherited methods. If you want to have a method foo(struct parent_class bar) and call it with foo((struct parentclass) derived_class), this will work correctly. The C-Standard defines this. Thus, I'd generally prefer method #2. In general, it is guaranteed that casting a structure to its first member will result in a struct containing the data of the first member of the struct, no matter how memory is laid out.

提交回复
热议问题