String Literal to Class Constructor and Const References

Hi, I have a question, let me explain...

Instance one: string object created in main, another temporary copy is created in a different section of memory, then the area of memory allocated to m_Name is filled with the string value passed to it by the member initialiser.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Player
{
public:
	Player(string name);
private:
	string m_Name;
};
Player::Player(string name) :
	m_Name(name) {}

int main() {
	string name = "sam";
	Player sam(name);


Instance two: adding "&" to the string member initialiser for Player, makes it accept a reference to string name in main(), and passes the original value of name in main() over rather than creating an additional copy first.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Player
{
public:
	Player(string& name);
private:
	string m_Name;
};
Player::Player(string& name) :
	m_Name(name) {}

int main() {
	string name = "sam";
	Player sam(name);


Instance three(/four): However, if I try and pass a string literal to the the object on initialisation in main(), i.e.
 
Player sam("name");

... it won't work - is this because it wishes to create a reference to a memory address that doesn't exist yet? I don't entirely understand, because if I change the class constructor to:
1
2
Player::Player(const string& name) :
	m_Name(name) {}

... I can now pass string literals directly. I don't understand why, if someone could succinctly explain this to me I'd be so grateful. I'm sure it's quite simple but I've been banging my head against a wall trying to understand this concept, thanks!
Last edited on
... it won't work - is this because it wishes to create a reference to a memory address that doesn't exist yet?

I'm not understanding your question. In both snippets, name is instantiated before sam.

Either snippet should work. It doesn't matter if name is passed to the constructor by value or by reference (other than passing by reference is more efficient and allows passing changes back to the caller).

Both snippets compile correctly for me.

Line 1: Your class name is declared as Player1, while your constructor and references are to Player.
Hi, thanks for your reply. "Player1" was just a typo writing it up in the post.

1
2
3
4
5
6
Player::Player(string& name) :
	m_Name(name) {}

int main()
{
Player sam(string name = "Sam");


Would work, but I can't send it a literal ... I hear this is because it must be const because modifying the value of a literal doesn't make sense. Does that mean, therefore that:

1
2
3
4
Player::Player(const string& name) :
	m_Name(name) {}
...
	Player sam("Sam");


... means when I initialise a new Player object with the literal "Sam", a temporary memory location is created containing "Sam", and then a const string reference "points" to that same location, which I can then pass to m_Name?

Trying hard to be clear as to my meaning, which is difficult because I'm not sure I understand what I'm talking about precisely (which is why I'm here and trying to figure it out :))

Thanks

Edit: Oh, and when the constructor has finished, is the memory address containing the literal then deleted? Leaving me only with the memory address of m_Name for the new player object containing the string data?
Last edited on
a temporary memory location is created containing "Sam"

Generally not. There is a section of your object file that contains constant character strings ("Sam" is implicitly type const char *). This section of memory is loaded when your object file is loaded. A reference to "Sam" resolves to the address of the character literal in the constant text area. The constant text area exists for the duration of your program.
Topic archived. No new replies allowed.