开发者

Structure inheritance in C

开发者 https://www.devze.com 2023-01-23 17:50 出处:网络
Is it possible to inherit开发者_如何转开发 structure with another in standard C or C++?You can embed a structure inside another to simulate inheritance in C:

Is it possible to inherit开发者_如何转开发 structure with another in standard C or C++?


You can embed a structure inside another to simulate inheritance in C:

typedef struct {
    int i;
} base;

void basefunc(base *b);

typedef struct {
    base b;
    char c;
} extended;

extended e;
/* Initialise extended here */
basefunc(&e.b);      /* Use the type checker */
basefunc((base*)&e); /* Just make sure you know what you're doing */


C does not support inheritance.

C++ does support inheritance.


The only difference between struct and class is default visibility of members and default inheritance mode. struct D : B { ... is equivalent to class D : public B { public: ....

0

精彩评论

暂无评论...
验证码 换一张
取 消