抽象基类、私有继承和多重公共继承

Abstract Base Class, private inheritance and multiple public inheritance

本文关键字:继承 抽象 基类      更新时间:2023-10-16

我有一个问题或误解。这里我有一些ABC (Person)和两个私有派生的虚拟类(PokerPlayerGunslinger)。到这一部分,一切工作完美,直到我不得不声明一个公共类(BadDude)派生自(PokerPlayerGunslinger)。问题是=>。

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
{ /*...*/ };