这是真的
Warrior
没有成员
m_damage
Soldier
只有
士兵
你应该允许
s构造函数将损坏参数作为参数,并传递所需的值
m_损坏
#include <iostream>
class Soldier{
protected:
const int m_damage;
public:
// explicit will prevent implicit conversions from
// being permitted when constructing Soldier
// see http://en.cppreference.com/w/cpp/language/explicit
explicit Soldier(int damage=5):
m_damage(damage)
{}
int damage() const
{
return m_damage;
}
};
class Warrior: public Soldier{
public:
Warrior()
: Soldier(10)
{}
};
// lets make another subclass which implicitly uses the
// default Soldier constructor.
class Samurai: public Soldier{
public:
Samurai()
{}
};
int main(){
Warrior w;
Samurai s;
std::cout << w.damage() << '\n';
std::cout << s.damage() << '\n';
}