First of all I would like to say I am beginner and I did some research on this issue
I created five classes superbase
, base
and 3 derived:
class superbase
{
QList<base*> listofbase; //Composition relation between super base and base
// some other attributes
public:
superbase(); // or other overloaded constructor
};
class base
{
int i;
public:
base();
};
class derived1 : // Which inheritance should be used with base
{
int j;
public:
derived1(); // or other overloaded constructor
};
class derived2 : // Which inheritance should be used with base
{
int k;
public:
derived2(); // or other overloaded constructor
};
class derived3: // Which inheritance should be used with base
{
int l;
public:
derived3(); // or other overloaded constructor
};
superbase
andbase
have composition relationderived1
,derived2
,derived3
inherit only frombase
notsuperbase
- There are no methods the classes
- I also tried virtual inheritance, but I am not getting it properly, as everywhere people mention about a "diamond problem" but this is not the same.
My Task
- I am supposed to create multiple objects of
base
class as private attribute ofsuperbase
class(QList< base* > listofbase;
). Each object of base class can contain multiple objects of derived classes (derived1
,derived2
orderived3
).
Questions:
- How can I create objects for the derived class so that all derived class share only one single copy of base class object ?
- Which inheritance must be used in derived classes to generate only single copy of base object?