【发布时间】:2016-11-26 18:06:31
【问题描述】:
我有一个名为game的类,这是它的operator=的代码:
Game& Game::operator=(const Game &other){
if(this==&other){
return *this;
}else{
for(unsigned i=0;i<other.players.size();i=i+1){
Player* copy;
int str= other.players.at(i)->getStr();
if(str==1)
copy = new PlayerType1(dynamic_cast<const PlayerType1&>(*other.players.at(i)));
if(str==2)
copy = new PlayerType2(dynamic_cast<const PlayerType2&>(*other.players.at(i)));
if(str==3)
copy = new PlayerType3(dynamic_cast<const PlayerType3&>(*other.players.at(i)));
if(str==4)
copy = new PlayerType4(dynamic_cast<const PlayerType4&>(*other.players.at(i)));
players.push_back(copy);
}
winners = other.winners;
state = vector<string>(other.state);
deck = Deck(other.deck);
verbal = other.verbal;
highestNum = other.highestNum;
turnNum = other.turnNum;
currPlayer = other.currPlayer;
lastAsker = other.lastAsker;
lastAskee = other.lastAskee;
lastAskedCard = other.lastAskedCard;
return *this;
}
}
我试着在这里称呼它:
char* cf= "../src/config1.txt";
Game* game = new Game(cf);
game->init();
Game game2=*game;
game->play();
game2.printState();
在这种情况下,我的 operator= 将不会被使用。 但是如果 game2 已经被初始化,例如这里:
Game* game = new Game(cf);
game->init();
Game game2=*(new Game());
game2=*game;
game->play();
game2.printState();
知道可能是什么问题吗?
【问题讨论】:
标签: c++ operator-overloading operators