这可能是显而易见的,但我有点难过。我的向量表现不佳时遇到了一些麻烦,现在看来我找到了罪魁祸首。Player
这是我班的淡化版本。
class Player {
private:
std::string _firstName;
std::string _lastName;
public:
Player(std::string firstName, std::string lastName) {
_firstName = firstName;
_lastName = lastName;
};
Player(const Player& otherPlayer) {
_firstName = otherPlayer._firstName.c_str();
_lastName = otherPlayer._lastName.c_str();
std::cout << "Created " << _firstName << " " << _lastName << std::endl; // Why doesn't _firstName and _lastName contain anything?
};
std::string GetName() { return _firstName + " " + _lastName; };
};
int main(int argc, const char * argv[])
{
Player player1 = Player("Bill", "Clinton");
Player player2 = Player(player1);
std::cout << "Player: " << player2.GetName() << std::endl;
return 0;
}
输出是微薄的Player:
。我不确定为什么我的复制构造函数没有做我想要它做的事情,特别是考虑到这样的建议( Zac Howland 的评论说明了c_str();
-part)。我是否违反了三规则(顺便说一句,我还没有完全理解)?如果有人能指出我正确的方向,我将不胜感激!