【发布时间】:2015-12-30 00:04:25
【问题描述】:
我有一个类似的设置。从包含受保护成员 x 的单个基类的多级继承。
class Sprite {
protected:
float x, y;
}
class AnimatedSprite : Sprite {
public:
void draw(float x, float y);
}
class Player : AnimatedSprite {
public:
void draw(float x, float y);
}
派生类 Player 中的方法 draw 的实现是这样的。
void Player::draw(float x, float y) {
AnimatedSprite::draw(this->x, this->y);
}
但是编译器抱怨成员 x 和 y 不可访问,即使它们在基类中被列为受保护。
【问题讨论】:
-
为什么要使用私有继承?
-
@curiousguy:我猜是因为他不知道类的默认继承级别。
-
"编译器在抱怨" 那么你应该发布错误消息而不是解释它
-
@curiousguy 因为老实说,我对错误的简化解释提供了与实际错误消息相同的信息,但格式更易读。不过下次会记住的。