一个多重继承的问题

bird528 2008-09-02 06:32:50
请问如果一个派生类多重继承于不同的基类。 但是在不同的基类中有相同名字的成员函数。 那对于派生类来说, 如何使用和区别这来自两个不同基类的相同名字的函数呢。 多谢
...全文
182 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
tsst 2008-09-02
  • 打赏
  • 举报
回复
典型的多重继承二义性问题,采用virtual继承或者加作用域进行标记说明
elegant87 2008-09-02
  • 打赏
  • 举报
回复
多态!把基类的成员函数设为虚函数,派生类的对象就可以访问!
1L说的很好啊!
zclever 2008-09-02
  • 打赏
  • 举报
回复
那么,方法就很清楚了。使用虚拟继承,或者使用类域操作符
帅得不敢出门 2008-09-02
  • 打赏
  • 举报
回复
使用虚拟成员函数
veloting 2008-09-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sunhuanwen 的回复:]
使用的话,如下
class BaseA;
class BaseB;
class c: public BaseA, public BaseB
{
BaseA::func();
BaseB::func();
}

即,函数名前面加上所属类的名字,以及“::”类的域名操作符。
[/Quote]
或者如你l楼所说
sunhuanwen 2008-09-02
  • 打赏
  • 举报
回复
使用的话,如下
class BaseA;
class BaseB;
class c: public BaseA, public BaseB
{
BaseA::func();
BaseB::func();
}

即,函数名前面加上所属类的名字,以及“::”类的域名操作符。
pluminsnow 2008-09-02
  • 打赏
  • 举报
回复
如一楼所说
budweiser 2008-09-02
  • 打赏
  • 举报
回复
#include <iostream> 
using namespace std;

class Vehicle
{
public:
Vehicle(int weight = 0)
{
Vehicle::weight = weight;
}
void SetWeight(int weight)
{
cout<<"重新设置重量"<<endl;
Vehicle::weight = weight;
}
virtual void ShowMe() = 0;
protected:
int weight;
};
class Car:public Vehicle//汽车
{
public:
Car(int weight=0,int aird=0):Vehicle(weight)
{
Car::aird = aird;
}
void ShowMe()
{
cout<<"我是汽车!"<<endl;
}
protected:
int aird;
};

class Boat:public Vehicle//船
{
public:
Boat(int weight=0,float tonnage=0):Vehicle(weight)
{
Boat::tonnage = tonnage;

}
void ShowMe()
{
cout<<"我是船!"<<endl;
}
protected:
float tonnage;
};

class AmphibianCar:public Car,public Boat//水陆两用汽车,多重继续的体现
{
public:
AmphibianCar(int weight,int aird,float tonnage)
:Vehicle(weight),Car(weight,aird),Boat(weight,tonnage)
//多重继续要注重调用基类构造函数
{

}
void ShowMe()
{
cout<<"我是水陆两用汽车!"<<endl;
}
};
int main()
{
AmphibianCar a(4,200,1.35f);//错误
a.SetWeight(3);//错误
system("pause");
}


以上是你提出的问题,
编译报错 :
error C2614: 'AmphibianCar' : illegal member initialization: 'Vehicle' is not a base or member
error C2385: 'AmphibianCar::SetWeight' is ambiguous
warning C4385: could be the 'SetWeight' in base 'Vehicle' of base 'Car' of class 'AmphibianCar'
warning C4385: or the 'SetWeight' in base 'Vehicle' of base 'Boat' of class 'AmphibianCar'
warning C4508: 'main' : function should return a value; 'void' return type assumed

大概意思就是有重复的 SetWeight 函数定义

解决这个问题,可以通过虚拟继承, 带那如下:

#include <iostream> 
using namespace std;

class Vehicle
{
public:
Vehicle(int weight = 0)

{
Vehicle::weight = weight;
cout<<"载入Vehicle类构造函数"<<endl;
}
void SetWeight(int weight)
{
cout<<"重新设置重量"<<endl;
Vehicle::weight = weight;
}
virtual void ShowMe() = 0;
protected:
int weight;
};
class Car:virtual public Vehicle//汽车,这里是虚拟继续
{
public:
Car(int weight=0,int aird=0):Vehicle(weight)
{
Car::aird = aird;
cout<<"载入Car类构造函数"<<endl;
}
void ShowMe()
{
cout<<"我是汽车!"<<endl;
}
protected:
int aird;
};

class Boat:virtual public Vehicle//船,这里是虚拟继续
{
public:
Boat(int weight=0,float tonnage=0):Vehicle(weight)
{
Boat::tonnage = tonnage;
cout<<"载入Boat类构造函数"<<endl;
}
void ShowMe()
{
cout<<"我是船!"<<endl;
}
protected:

float tonnage;
};

class AmphibianCar:public Car,public Boat//水陆两用汽车,多重继续的体现
{
public:
AmphibianCar(int weight,int aird,float tonnage)
:Vehicle(weight),Car(weight,aird),Boat(weight,tonnage)
//多重继续要注重调用基类构造函数
{
cout<<"载入AmphibianCar类构造函数"<<endl;
}
void ShowMe()
{
cout<<"我是水陆两用汽车!"<<endl;
}
void ShowMembers()
{
cout<<"重量:"<<weight<<"顿,"<<"空气排量:"<<aird<< "CC,"<<"排水量:"<<tonnage<<"顿"<<endl;
}
};
int main()
{
AmphibianCar a(4,200,1.35f);
a.ShowMe();
a.ShowMembers();
a.SetWeight(3);
a.ShowMembers();
system("pause");
}


就是在继承列表的前面加上 virtual 关键字 。
Learn-anything 2008-09-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sunhuanwen 的回复:]
使用的话,如下
class BaseA;
class BaseB;
class c: public BaseA, public BaseB
{
BaseA::func();
BaseB::func();
}

即,函数名前面加上所属类的名字,以及“::”类的域名操作符。
[/Quote]

个人感觉这个方法不错
e_sharp 2008-09-02
  • 打赏
  • 举报
回复
virtual继承 或者 ::类域操作符
jinwei1984 2008-09-02
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 tsst 的回复:]
典型的多重继承二义性问题,采用virtual继承或者加作用域进行标记说明
[/Quote]
家有萌宝V 2008-09-02
  • 打赏
  • 举报
回复
采用纯虚函数将要继承的基类类声明成抽象类是一个不错的方法

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧