[英]How to inherit only a method of a class, not the whole class itself (C++)
我有两个类 - Deck
和Player
,结构如下:
class Deck {
public:
//constructors, destructors, operator=
void addCard(const Card &card);
bool changeCard(const unsigned int &index, const Card &card);
void clearDeck();
//other things
private:
std::vector<Card *> cards;
};
class Player {
public:
//constructors, destructors, operator=
void changeCardInDeck(const unsigned int &index, const Card &card);
void addCardInDeck(const Card &card);
//other things
private:
std::string name;
Deck *deck;
};
如您所见,由于Player
class 有一个Deck
成员,它包含与Deck
class 相同的方法。 但是做这样的事情:
void Player::addCardInDeck(const Card &card) {
this->deck->addCard(card);
}
我认为你当前的设计很好,但是如果你真的想要,你可以在你的甲板 class, private中制作你不想访问的方法,如果你有一些 class 想要能够访问那个私有方法,你让 class 成为朋友class。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.