This is probably a no-brainer, but I'm a little stumped. I've had some trouble with my vectors not behaving nicely, and now it looks like I've found the culprit. Here's a watered down version of my Player
class.
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;
}
The output is a meager Player:
. I'm not sure why my copy constructor doesn't do what I want it to do, in particular in light of advice such as this (Zac Howland's comment accounts for the c_str();
-part). Am I violating the rule of three (which, btw, I still haven't totally gotten my head around)? I'd be really grateful if anyone could point me in the right direction!