我如何分配一个指针到一个类的多重继承
how do I allocate a pointer to a class with multiple inheritance
假设我有:
class Human {
string choice;
public:
Human(string);
};
class Computer {
string compChoice;
public:
Computer(string);
};
class Refree : public Human, public Computer {
public:
string FindWinner();
};
int main() {
Human* H1 = new Human("name");
Computer* C1 = new Computer("AI");
Refree* R1 = new Refree();
}
使用
编译失败 In function 'int main()':
error: use of deleted function 'Refree::Refree()'
note: 'Refree::Refree()' is implicitly deleted because the default definition would be ill-formed:
error: no matching function for call to 'Human::Human()'
note: candidates are:
note: Human::Human(std::string)
note: candidate expects 1 argument, 0 provided
为什么这个失败,我如何构造一个指向Refree
的指针?
由于Human
和Computer
有用户声明的带实参的构造函数,它们的默认构造函数被隐式删除。为了构造它们,你需要给它们一个参数。
然而,您试图在没有任何参数的情况下构建Refree
-它隐含地试图在没有任何参数的情况下构建其所有基。那是不可能的。抛开Human
和Computer
是否有意义不谈,至少你必须做这样的事情:
Refree()
: Human("human name")
, Computer("computer name")
{ }
更可能的情况是,您希望提供一个接受一个或两个名称的构造函数,例如:
Refree(const std::string& human, const std::string& computer)
: Human(human)
, Computer(computer)
{ }
相关文章:
- 如何从另一个文件继承私有成员变量和公共函数
- 关于C++中具有多重继承"this"指针的说明
- C++中模板化异常类的多重继承
- 虚拟继承中是否存在多重继承?
- 如何在 c++ 多重继承中调用父非虚函数?
- 多重继承相同的方法名,没有歧义
- 一个关于继承和引用的C++问题
- 使用enable_if解决多重继承歧义
- 多重继承导致虚假的模糊虚拟函数过载
- C++多重继承 - 相同的方法名称 - 我可以以某种方式删除其中一个吗?
- 为什么在多重继承的情况下,QObject需要成为第一个
- 使用一个基类进行多重继承
- 从一个基类的指针对另一个基类的多重继承和泛型访问
- 多重继承.如何将 1 个父类的成员函数访问到另一个父类
- 防止一个类的派生类对两个类进行多重继承
- 如何在多重继承下从一个基类继承所有类型
- 为具有多重继承的一个类型设置默认构造函数
- 在c++中获取一个类的指针,该类是另一个具有多重继承的类的一部分
- 一个c++问题,涉及多重继承、模板和静态变量
- 我如何分配一个指针到一个类的多重继承