[英]C++ multiple inheritance
Please don't question the really odd hierarchy of workers in this code here, I have no idea why anyone would want something like this, but I decided to give myself an exercise in Multiple Inheritance, just to be sure I fully understood it. 请不要在这里质疑此代码中真正奇怪的工人层次结构,我不知道为什么有人会想要这样的东西,但是我决定在“多重继承”中做一个练习,以确保我完全理解它。 So here's the result.
这就是结果。
using namespace std;
class Employee
{
protected:
string name;
public:
string getname()
{
return name;
}
void setname(string name2)
{
name = name2;
}
Employee(string name2)
{
name = name2;
}
Employee(){}
};
class Manager : public Employee
{
public:
string getname()
{
return ("Manager" + name);
}
Manager(string name2) : Employee(name2){}
Manager() : Employee(){}
};
class Supervisor : public Manager,public Employee
{
public:
Supervisor(string name2) : Manager(name2) , Employee(name2){}
Supervisor() : Manager() , Employee(){}
string getname()
{
return ("Supervisor" + Employee::getname());
}
};
Hopefully you're understanding what I'm trying to do here. 希望您能理解我在这里要做的事情。 I'm getting something about an "ambiguous conversion between derived class 'Supervisor' and base class 'Employee.'" So what do I do?
我正在获得“派生类'Supervisor'和基类'Employee'之间的模棱两可转换”的信息。那我该怎么办?
Actually, the way you have defined Supervisor
class, its object will have two subjects of type Employee
, each coming from it base classes. 实际上,按照您定义
Supervisor
类的方式,其对象将有两个类型为Employee
主题,每个主题都来自它的基类。 That is causing problem. 这引起了问题。
The solution is to use virtual inheritance (assuming you need multiple inheritance) as: 解决方案是使用虚拟继承 (假设您需要多重继承):
class Manager : public virtual Employee
Hope you note the virtual
keyword here. 希望您在这里记下
virtual
关键字。 :-) :-)
You need a virtual inheritance in this case: 在这种情况下,您需要虚拟继承:
#include <iostream>
#include <string>
using namespace std;
class Employee
{
protected:
string name;
public:
string getname()
{
return name;
}
void setname(string name2)
{
name = name2;
}
Employee(string name2)
{
name = name2;
}
Employee(){}
};
class Manager : public virtual Employee
{
public:
string getname()
{
return ("Manager" + name);
}
Manager(string name2) : Employee(name2){}
Manager() : Employee(){}
};
class Supervisor : public Manager,public virtual Employee
{
public:
Supervisor(string name2) : Manager(name2) , Employee(name2){}
Supervisor() : Manager() , Employee(){}
string getname()
{
return ("Supervisor" + Employee::getname());
}
};
This problem is also known as Diamond inheritance problem: http://en.wikipedia.org/wiki/Diamond_problem 此问题也称为钻石继承问题: http : //en.wikipedia.org/wiki/Diamond_problem
Everybody has already covered virtual inheritance, but I'd suggest reading the C++ FAQ as well. 每个人都已经讨论过虚拟继承,但是我建议您也阅读C ++ FAQ。
http://www.parashift.com/c++-faq-lite/multiple-inheritance.html http://www.parashift.com/c++-faq-lite/multiple-inheritance.html
Supervisor
contains two objects of type Employee, the direct one and the one over Manager. Supervisor
包含两个类型为Employee的对象,直接对象和上一个Manager对象。 It is ambiguous to call Employee
methods on a Supervisor
in consequence ( which Employee should be called?). 结果是在
Supervisor
上调用Employee
方法是模棱两可的(应该调用哪个 Employee?)。 You might want to employ virtual multiple inheritance instead. 您可能想改用虚拟多重继承。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.