How to Add New Members to Struct
These are functions and Struct declarations I have, and I'm not allowed to change them.
DerivedA giveDerivedA ();
DerivedB giveDerivedB ();
struct Base{
QString elementId;
QString elementType;
};
struct DerivedA : Base {
int a;
int b;
};
struct DerivedB : Base {
int c;
int d;
};
But what I need is something like this:
struct DerivedA : Base {
int a;
int b;
void cre开发者_如何学Cate();
QString doc;
};
How can I add these method and member to structs I got?
My first idea is:
struct myA: DerivedA {
void create();
QString doc;
};
Do you have any suggestion?
Edit: 2nd Alternative(Choosed)
struct myA{
void create();
QString doc;
private:
DerivedA derivedA;
};
This is similar to the problem people have extending standard library classes. If your base class doesn't have a virtual destructor, you can't safely inherit from it. In that case, you must either use free-functions (preferred anyway), or composition.
Otherwise, what you have there is good.
Use composition or inheritance, depending on what kind of relationship the classes have (see e.g. Items 32 and 38 in Effective C++).
That ain't workin', that's the way you do it.
精彩评论