C中的结构inheritance
是否可以在标准C或C ++中inheritance另一个结构?
C不支持inheritance。
C ++确实支持inheritance。
您可以在另一个内部嵌入一个结构来模拟C中的inheritance:
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 */
struct
和class
之间的唯一区别是成员的默认可见性和默认inheritance模式。 struct D : B { ...
等同于class D : public B { public: ...