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: ...
.
精彩评论