从多重继承中的双亲调用函数

Call function from both parent in multiple Inheritance

本文关键字:调用 函数 多重继承      更新时间:2023-10-16

EDIT:很难用英语解释自己,所以我会尝试使用"示例"代码。

class Character {
    public:
        Character(void); //constructor
        int     getAttack(void) const; // return this->_attack;
        int     getDef(void) const; // return this->_def;
        int     getSpeed(void) const; // return this->_speed;
        void    setAttack(int val); //this->_attack = val;
        void    setDef(int val); //this->_def = val;
        void    setSpeed(int val); //this->_speed = val;
    private:
        int     _attack;
        int     _def;
        int     _speed;
};

Character::Character(void) : _attack(1), _def(1), _speed(1) {
    return ;
}

字符统计为:1/1/1


class Strong : public virtual Character {
    public:
        Strong(void); //constructor
};
Strong::Strong(void) : Character() {
    this->setAttack(100);
    this->setDef(100);
    this->setSpeed(1);
    return;
}

强大的统计数据是:100/100/1


class Quick : public virtual Character {
    public:
        Quick(void); //constructor
};
Quick::Quick(void) : Character() {
    this->setAttack(1);
    this->SetDef(1);
    this->setSpeed(100);
    return;
}

快速统计数据为:1/1/100


class Super : public Strong, public Quick {
    public:
        Quick(void); //constructor
};
Super::Super(void) : Character(), Strong(), Quick() {
    this->setAttack(Strong::getAttack());
    this->setDef(Strong::getDef());
    this->setSpeed(Quick::getSpeed());
    return;
}

超级统计总是等于快速(或强大,如果我交换订单)。

知道为什么吗?

如何初始化Super的Attack&请根据其父"Strong"的值进行定义,并根据Quick的速度值初始化他的速度值?

您应该使用作用域解析运算符::

例如,如果您想调用函数的Man版本,请使用both.Man::punch("bad guy"),对于另一个版本,请类比使用both.Woman::punch("bad guy")

Edit:现在您的编辑更改了问题,您的需求又回到了不明确的状态。但这里有一种(可能有些过头了)替换无用代码的方法,我在最初的回答中对此进行了评论(我在下面留下了)。

Strong strong;  // Local variables to copy info from
Quick quick;
setAttack(strong.getAttack());
setDef(strong.getDef());
setSpeed(quick.getSpeed());

(之前的答案,在问题更改之前)

这些操作都没有任何作用:

this->setAttack(Strong::getAttack());
this->setDef(Strong::getDef());
this->setSpeed(Quick::getSpeed());

规范Strong::Quick::在这方面没有任何作用,因为三个get函数都来自Character的同一副本,而不管它们是以何种方式继承的。三个set调用只是重写读取的值。

但是,不管有没有这些无用的行,代码都应该做你想要的,而不是你报告的。Character构造函数在StrongQuick之前只调用一次,因此这是存储1值的唯一点。然后,StrongQuick构造函数中的每一个(按照您定义的顺序)都被称为将这些1 s更改为100 s。

所以,如果你的测试结果不是这样,那么问题出在你没有发布的代码中。请尝试发布测试的完整代码。

下面是对你发布的内容的测试,去掉了无用的代码,剩下的部分完成了一个最小的测试程序:

你问:

如何初始化Super的Attack&无视他的价值观父级"Strong",并从的速度值初始化他的速度值快速

正如您通过运行此代码所看到的,这就是您的代码已经工作的方式。注意(从我添加的cout中)构造函数执行的顺序。注意(也可以从这些cout的输出中注意到,Character的三个数据成员只存在于对象中的一个位置,并由构造函数按顺序进行增量修改。

#include <iostream>

class Character {
    public:
        Character(void); //constructor
        int     getAttack(void) const { return this->_attack; }
        int     getDef(void) const { return this->_def; }
        int     getSpeed(void) const { return this->_speed; }
        void    setAttack(int val) {this->_attack = val; }
        void    setDef(int val) {this->_def = val;}
        void    setSpeed(int val) {this->_speed = val; }
    private:
        int     _attack;
        int     _def;
        int     _speed;
};

Character::Character(void) : _attack(1), _def(1), _speed(1) {
    std::cout << "Character: "<< getAttack() << " " << getDef() << " "<< getSpeed() << std::endl;
}
class Strong : public virtual Character {
    public:
        Strong(void); //constructor
};
Strong::Strong(void) : Character() {
    this->setAttack(100);
    this->setDef(100);
    std::cout << "Strong: "<< getAttack() << " " << getDef() << " "<< getSpeed() << std::endl;
}
class Quick : public virtual Character {
    public:
        Quick(void); //constructor
};
Quick::Quick(void) : Character() {
    this->setSpeed(100);
    std::cout << "Quick: "<< getAttack() << " " << getDef() << " "<< getSpeed() << std::endl;
}
class Super : public Strong, public Quick {
    public:
        Super(void); //constructor
};
Super::Super(void) : Character(), Strong(), Quick() {
    std::cout << "Super: "<< getAttack() << " " << getDef() << " "<< getSpeed() << std::endl;
}
int main()
{
  Super test;
  return 0;
}