0

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 and base have composition relation
  • derived1, derived2, derived3 inherit only from base not superbase
  • 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 of superbase class( QList< base* > listofbase; ). Each object of base class can contain multiple objects of derived classes (derived1, derived2 or derived3).

Questions:

  1. How can I create objects for the derived class so that all derived class share only one single copy of base class object ?
  2. Which inheritance must be used in derived classes to generate only single copy of base object?
4

1 回答 1

-1

使用静态工厂和受保护的构造函数在基类中实现这一点。

于 2013-03-01T14:05:49.083 回答