从多重继承中的双亲调用函数
Call function from both parent in multiple Inheritance
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
构造函数在Strong
或Quick
之前只调用一次,因此这是存储1
值的唯一点。然后,Strong
和Quick
构造函数中的每一个(按照您定义的顺序)都被称为将这些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;
}
- 如何用参数值调用函数(仅在运行时已知)
- 从python中调用C++函数并获取返回值
- 当使用通配符和null指针调用函数时,对输出的说明
- 从R调用C++函数并对其进行集成时出错
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 如何在qt中从另一个类调用函数
- 在 COUT 语句中使用 COUT 调用函数
- 如何从线程中的不同模块调用函数?
- C++从函数指针数组调用函数
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- 如何在 C/C++ 中从外部库调用函数
- 如何使用运算符在同一行中多次调用函数
- 是否可以创建一个从不同类调用函数的线程?
- 无法为类成员调用函数
- 如何从另一个标头 c++ 调用函数
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 如何只允许在调用函数 B 后调用函数 A?
- 我可以这样调用函数吗?
- 如何在 c++ 的类中递归调用函数方法?
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?