和虚拟继承不一样,虚拟继承需要些虚基类的构造函数。

虚拟继承构造函数写法:http://www.cnblogs.com/vhyc/p/5582450.html

class A{
protected:
	int a;
public:
	A(int x) :a(x){ cout << "a" << endl; };
};

class B :public A
{
protected:
	int b;
public:
	B(int x, int y) :A(x), b(y){ cout << "b" << endl; }
};

class C :public B
{
private:
	int c;
public:
	C(int x, int y, int z) :B(x, y), c(z){ cout << "c" << endl; }//和虚拟继承不一样,不能写A(x),否则编译就不过关
	void display()
	{
		cout << a << endl << b << endl << c << endl;
	}
};

int main()
{
	C a(1, 2, 3);
	a.display();
}

  

相关文章:

  • 2022-12-23
  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
  • 2022-01-06
猜你喜欢
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2022-01-24
  • 2022-12-23
  • 2021-08-30
  • 2021-10-24
相关资源
相似解决方案