这是正确的方法来使用数组的抽象类(c++)
Is this the correct way to use an array of abstract class (C++)?
忽略我使用原始指针/数组的事实。
我正在用c++做一个纸牌游戏,它有一个Player
抽象类,由其他类扩展。我需要创建一个指向这些派生类的指针数组。这是有效的,但是有更好的方法吗?
class Player{
public:
Player();
Player(const Player&);
Player & operator=(const Player &);
virtual void sort() = 0;
virtual Card play(Pile*) = 0;
void confirmPlay();
void giveHand(Hand&);
void giveCard(Card);
bool hasCards();
void won();
void resetWins();
int getWins();
virtual ~Player();
protected:
int cardsLeft;
Hand hand;
int cardPlayed;
int wins;
private:
};
class DumbPlayer : public Player
{
public:
DumbPlayer();
Card play(Pile*);
void sort();
virtual ~DumbPlayer();
DumbPlayer & operator=(const DumbPlayer &);
DumbPlayer(const DumbPlayer&);
protected:
private:
};
class Game{
public:
Game(int, Player**&);
void playX(int);
void playOne();
virtual ~Game();
protected:
private:
bool done();
Game & operator=(const Game &);
Game(const Game&);
Pile * piles;
Deck deck;
int playerCount;
Player ** players;
};
//implementation not shown to save space, tell me if you would like to see anything.
//I think that all of the other class/function names should be good enough to not show.
Game::Game(const int pplayerCount, Player**& pplayers) :
piles(new Pile[4]),
playerCount(pplayerCount),
deck(),
players(new Player*[playerCount]){
for(int i = 0; i < 4; i++){
piles[i].setSuit(i);
}
for(int i = 0; i < playerCount; i++){
players[i] = pplayers[i];
}
}
void Game::playOne(){
deck.shuffle(); //shuffle deck
for(int i = 0; i < 4; i++){
piles[i].reset(); //reset piles
}
Hand hands[playerCount]; //create hands
Hand leftovers;
deck.dealAll(playerCount, hands, leftovers); //deal the deck
int cardsLeftover = leftovers.getSize(); //there are leftover cards,
//52/3 has a remainder
for(int playerIdx = 0; playerIdx < playerCount; playerIdx++){
(*players[playerIdx]).giveHand(hands[playerIdx]); //this is what
//i am unsure about.
(*players[playerIdx]).sort();
}
int winner = -1;
while(!done()){
for(int playerIdx = 0; playerIdx < playerCount; playerIdx++){
Card play = (*players[playerIdx]).play(piles);
if(piles[play.getSuit()].canPut(play)){
(*players[playerIdx]).confirmPlay();
piles[play.getSuit()].put(play);
if(!(*players[playerIdx]).hasCards()){
winner = playerIdx;
break;
}
} else {
if(cardsLeftover > 0){
(*players[playerIdx]).giveCard(leftovers.popCard(--cardsLeftover));
}
}
}
if(winner != -1){
(*players[winner]).won();
break;
}
}
}
我知道这是大量的代码(对于这个网站)…我不确定游戏构造器/类和行包括(*players[i]).x()
现在,指针数组的拼写是:
std::vector<boost::shared_ptr<Player> > players;
这充分利用了资源获取即初始化(RAII)习语,以确保在面对异常和不可预见的代码路径时正确调用析构函数并回收内存。
使用std::vector, list, map等
相关文章:
- 如何在没有硬编码的情况下以C++为单位获取类数组的长度?
- 类数组插入函数
- C++访问指向结构的指针中的类数组指针
- 类数组正在损坏数据
- 如何在 turbo C++ 中增加类数组的大小?
- 如何初始化文件中的文本并将其传递给类数组,以便在c++中输出
- 从基类数组调用派生函数
- lower_bound结构节点的类数组中
- 在构造函数中使用参数创建一个类数组
- 有没有办法拥有一个未初始化的类数组,这些类在调用 delete[] 时不会被破坏?
- 使用泛型成员变量"placement new"结构/类数组的正确方法是什么?
- 如何将字符串片段读入类数组C++
- 如果类数组中的向量是动态push_backed的,内存如何工作
- C 类数组的Kotlin等效
- 指针算术,带有指向类数组的指针
- 具有多种类型的类数组?如何访问数组中的一个类型
- 尝试在类数组上使用成员函数
- C++17 std::shared_ptr<> 类数组对象的重载运算符 []
- 如何使用C样式在C 中正确复制抽象类数组的2D数组
- 在C++中初始化抽象基类的子类数组