菱形多重继承模式

发布于 2025-01-06 21:30:22 字数 2053 浏览 0 评论 0原文

下面是多重继承中面临的钻石问题,

class Base {
 public:
  Base() {
    cout << "Empty Base constructor " << endl; 
  }
  Base(const string & strVar) { 
    m_strVar = strVar; 
    cout << m_strVar << endl;
  }
  virtual ~Base() {
    cout << "Empty Base destructor " << endl;
  }
  virtual const string & strVar() const { 
   return m_strVar; 
  }

  string m_strVar;
};

class Derived1: public virtual Base {
 public:
  Derived1() {
    cout << "Empty Derived1 constructor " << endl; 
  }
  Derived1(const string & strVar) : Base(strVar) {
    cout << " Derived1 one arg constructor" << endl;
  }
  ~Derived1() {
    cout << "Empty Derived1 destructor " << endl;
  }
};

class Derived2: public virtual Base {
 public:
  Derived2() {
    cout << "Empty Derived2 constructor " << endl; 
  }
  Derived2(const string & strVar) : Base(strVar) {
    cout << "Derived2 one arg constructor" << endl;
  }
  ~Derived2() {
    cout << "Empty Derived2 destructor " << endl;
  }
};

class Derived: public Derived1, public Derived2 {
 public:
  Derived(const string & strVar) : Derived1(strVar), Derived2(strVar) {
    cout << "Derived Constructor " << endl; 
  }
  ~Derived() {
    cout << "Empty Derived destructor " << endl;
  }
};

int main() {
  Derived derObj ("Print this if you can ! "); 
}

我得到的输出是

  1. Empty Base constructor
  2. Derived2 one arg constructor
  3. Derived1 one arg constructor
  4. Derived Constructor
  5. Empty Derived destructor
  6. Empty Derived2 destructor
  7. Empty Derived1 destructor
  8. Empty Base destructor

我想知道为什么我的 derObj 参数即“Print this if you can" 不会被打印,并且输出不像

  1. Empty Base constructor
  2. Derived2 one arg 如果可以的话,打印
  3. 这个!
  4. Derived1 一个 arg 构造函数
  5. 派生构造函数
  6. 空 派生析构
  7. 函数 空 Derived2 析构函数
  8. 空 Derived1 析构函数
  9. 空 基类析构函数

Below is a diamond problem faced in multiple inheritance,

class Base {
 public:
  Base() {
    cout << "Empty Base constructor " << endl; 
  }
  Base(const string & strVar) { 
    m_strVar = strVar; 
    cout << m_strVar << endl;
  }
  virtual ~Base() {
    cout << "Empty Base destructor " << endl;
  }
  virtual const string & strVar() const { 
   return m_strVar; 
  }

  string m_strVar;
};

class Derived1: public virtual Base {
 public:
  Derived1() {
    cout << "Empty Derived1 constructor " << endl; 
  }
  Derived1(const string & strVar) : Base(strVar) {
    cout << " Derived1 one arg constructor" << endl;
  }
  ~Derived1() {
    cout << "Empty Derived1 destructor " << endl;
  }
};

class Derived2: public virtual Base {
 public:
  Derived2() {
    cout << "Empty Derived2 constructor " << endl; 
  }
  Derived2(const string & strVar) : Base(strVar) {
    cout << "Derived2 one arg constructor" << endl;
  }
  ~Derived2() {
    cout << "Empty Derived2 destructor " << endl;
  }
};

class Derived: public Derived1, public Derived2 {
 public:
  Derived(const string & strVar) : Derived1(strVar), Derived2(strVar) {
    cout << "Derived Constructor " << endl; 
  }
  ~Derived() {
    cout << "Empty Derived destructor " << endl;
  }
};

int main() {
  Derived derObj ("Print this if you can ! "); 
}

the output i get is

  1. Empty Base constructor
  2. Derived2 one arg constructor
  3. Derived1 one arg constructor
  4. Derived Constructor
  5. Empty Derived destructor
  6. Empty Derived2 destructor
  7. Empty Derived1 destructor
  8. Empty Base destructor

i wonder why my derObj parameter i.e "Print this if you can" is not printed and output is not like

  1. Empty Base constructor
  2. Derived2 one arg constructor
  3. Print this if you can!
  4. Derived1 one arg constructor
  5. Derived Constructor
  6. Empty Derived destructor
  7. Empty Derived2 destructor
  8. Empty Derived1 destructor
  9. Empty Base destructor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一人独醉 2025-01-13 21:30:22

这与虚拟继承有关。

当一个类被虚拟继承时,层次结构中最派生的类有责任调用其构造函数:这里是Derived

由于 Base 是默认可构造的,并且您没有精确任何内容,因此 Derived 会调用 Base 的默认构造函数。

如果您希望打印字符串,请使用:

Derived(const string & strVar) : Base(strVar), Derived1(strVar), Derived2(strVar)
{
  std::cout << "Derived Constructor\n"; 
}

您可以删除默认构造函数以使编译器诊断问题,但并非所有编译器都会提供非常有用的消息。

This has to do with virtual inheritance.

When a class is inherited virtually, it is the responsability of the most derived class in the hierarchy to call its constructor: here Derived.

Since Base is default constructible and you did not precise anything, Derived invokes the default constructor of Base.

If you want the string to be printed, use:

Derived(const string & strVar) : Base(strVar), Derived1(strVar), Derived2(strVar)
{
  std::cout << "Derived Constructor\n"; 
}

You could remove the default constructor to let the compiler diagnose the issue, though not all compilers give very helpful messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文