2
回答
C++包含内嵌对象多继承派生类的带参构造函数和析构函数调用顺序,出现一处错误,求解决
华为云实践训练营,热门技术免费实践!>>>   

出现错误:程序中有游离的。

是错在main函数那边的objD那里的。

#include <iostream>
using namespace std;
class Base1
{
public:
	Base1(int i) {cout<<"Base1 constructor called "<<i<<endl;}
	~Base1() {cout<<"Base1 destructor called"<<endl;}
};
class Base2
{
public:
	Base2(int i) {cout<<"Base2 constructor called "<<i<<endl;}
	~Base2() {cout<<"Base2 destructor called"<<endl;}
};
class Base3
{
public:
	Base3(int i) {cout<<"Base3 constructor called "<<i<<endl;}
	~Base3() {cout<<"Base3 destructor called"<<endl;}
};
class Derived:public Base2,public Base1,public Base3
{
Base3 member3;Base1 member1;Base2 member2;
public:
	Derived(int a,int b,int c,int d,int e,int f,int g):Base1(b),Base2(c),Base3(d),member1(e),member2(f),member3(g) 	{cout<<"Derived constructor called "<<a<<endl;} 	
	~Derived() {cout<<"Derived destructor called"<<endl;}
};
int main()
{
  Derived objD(1,2,3,4,5,6,7);
  return 0;
}

<无标签>
举报
caosz
发帖于4年前 2回/911阅
共有2个答案 最后回答: 4年前
#include <iostream>
using namespace std;
class Base1
{
public:
	Base1(int i) {cout<<"Base1 constructor called "<<i<<endl;}
	~Base1() {cout<<"Base1 destructor called"<<endl;}
};
class Base2
{
public:
	Base2(int i) {cout<<"Base2 constructor called "<<i<<endl;}
	~Base2() {cout<<"Base2 destructor called"<<endl;}
};
class Base3
{
public:
	Base3(int i) {cout<<"Base3 constructor called "<<i<<endl;}
	~Base3() {cout<<"Base3 destructor called"<<endl;}
};
class Derived:public Base2,public Base1,public Base3
{
Base3 member3;Base1 member1;Base2 member2;
public:
	Derived(int a,int b,int c,int d,int e,int f,int g):Base1(b),Base2(c),Base3(d),member1(e),member2(f),member3(g) 	
	{
		cout<<"Derived constructor called "<<a<<endl;
	} 	
	~Derived() {cout<<"Derived destructor called"<<endl;}
};
int main()
{
	Derived objD(1,2,3,4,5,6,7);
	return 0;
}
顶部