【发布时间】:2013-03-01 13:41:05
【问题描述】:
首先我想说我是初学者,我对这个问题做了一些研究
我创建了五个类superbase、base 和 3 个派生类:
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和base有组合关系 -
derived1,derived2,derived3仅继承自base不是superbase - 类没有方法
- 我也尝试过虚拟继承,但我没有正确理解它,因为到处有人提到“钻石问题”,但这不一样。
我的任务
- 我应该创建
base类的多个对象作为superbase类(QList< base* > listofbase;) 的私有属性。每个基类对象可以包含多个派生类对象(derived1、derived2或derived3)。
问题:
- 如何为派生类创建对象,以便所有派生类只共享一个基类对象的一个副本?
- 必须在派生类中使用哪种继承才能仅生成基对象的单个副本?
【问题讨论】:
-
为什么
superbase会出现在这里?您可以完全删除它,但仍然有同样的问题。 -
另外,您的任务是否要求您使用继承?这似乎不是正确的方法。
-
好吧,这个问题实际上是一个类图的实现.. 假设超基的名称是其他随机名称 A .. 在类 A 和 base 之间,存在组合关系 1-->1.. * 这就是为什么 superbase(A) 将 QList od 基指针作为私有属性。所以我不能省略 superbase(A) 类
-
是的,我需要使用继承,因为在该类图中,派生的 1、派生 2 和派生 3 是从类基派生的……但我不确定哪个继承以及如何使用,以制作单个副本基类对象。
-
我认为这是不可能的。有人告诉过你吗?
标签: c++ qt inheritance multiple-inheritance derived-class