抽象基类、私有继承和多重公共继承
Abstract Base Class, private inheritance and multiple public inheritance
我有一个问题或误解。这里我有一些ABC (Person
)和两个私有派生的虚拟类(PokerPlayer
和Gunslinger
)。到这一部分,一切工作完美,直到我不得不声明一个公共类(BadDude
)派生自(PokerPlayer
和Gunslinger
)。问题是=>。
1)
BadDude::BadDude(const BadDude & obj)
: Person::Person(obj) /*<- the problem*/, PokerPlayer(obj) , Gunslinger(obj) {}
2)对于operator=();
也是如此。
BadDude & BadDude::operator=(const BadDude & obj)
{
if (this == & obj)
return *this;
Person::operator=(obj); //dose not work
PokerPlayer::oprtr(obj);
Gunslinger::oprtr(obj);
return *this;
}
3)我无法从BadDude的方法中获取protected ABC的方法
void BadDude::Show()const
{
Person::Data(); //the same problem
Gunslinger::Data();
PokerPlayer::Data();
}
这是工作室对问题#1的回答
error C2436: '{ctor}' : member function or nested class in constructor initializer list
这是工作室对问题#2的回答
error C2249: 'Person::operator ='
: no accessible path to private member declared in virtual base 'Gunslinger'
这是工作室对问题#3的回答
error C2249: 'Person::Data' : no accessible path to private member declared in virtual base 'Gunslinger'
这里的代码在这里输入链接描述
对于问题1:有额外的Person::
,它应该是
BadDude::BadDude(const BadDude & obj) : Person(obj), PokerPlayer(obj) , Gunslinger(obj) {}
对于可访问性问题,使用受保护(或公共)继承:
class Gunslinger : virtual protected Person
{ /*...*/ };
class PokerPlayer : virtual protected Person
{ /*...*/ };
相关文章:
- 从抽象类继承以创建另一个抽象类时,我应该重新声明所有虚函数吗?
- 抽象类析构函数与继承"Shutdown"函数
- 工厂方法模式使用继承而抽象工厂模式使用组合如何
- C++ 多重继承:使用基类 A 的实现实现基类 B 的抽象方法
- C++的抽象类继承和构造函数的问题
- 自定义异常中的用户定义的空构造函数,具有多个继承和抽象基类
- C++:抽象类继承和构造
- 使用空基类强制继承抽象工厂的做法是不好的吗?
- 私下继承C++抽象基类是什么意思
- 抽象类 A 由类 C 继承"through"类 B "intermediary"。如何声明虚拟功能?
- 如何修复抽象类的迭代继承
- 具有继承类和函数的抽象类写入单独的向量c++
- 继承自C++中的抽象类
- C++抽象类和继承
- 继承抽象的基础接口及其实现给出了C2259
- 如何从 c++ 基类继承抽象行为
- 如何使用继承(抽象基类)实现移动构造函数和移动赋值运算符
- 多重继承抽象类
- C++ 继承抽象函数
- c++元编程:必须继承抽象类的模板参数