开发者

Structure 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜