1
votes
Person(const string &name)
{
    mName=name;
}


Person(string name)
{
    mName=name;
}

mName is a private member variable

So I made a class called Person. I'm wondering whats the difference between const string &name and string name. I have tried just putting string &name but it gave me an error message.

I know & is a reference so almost like an address? I'm guessing the const is needed because the reference is a constant? Also, why doesn't just string &name work?

This is the error message that I got

candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char [4]' to 'const Person' for 1st argument

candidate constructor not viable: no known conversion from 'const char [4]' to 'string &' (aka 'basic_string, allocator > &') for 1st argument Person(string &name)

4
Post the error message that it gives you when trying to use a non-const reference.Yuushi
the error message is related to the data type of mName. What data type have you used, Can you post the full code?vathsa

4 Answers

3
votes

Option #1 - pass the argument by-value, with Person(string name):

  1. string constructor is called, and a temporary string instance is created on the stack.

  2. Person constructor is called.

  3. string destructor is called with the temporary instance.

Option #2 - pass the argument by-reference, with Person(const string& name):

  1. A reference to the string instance (its 4-byte or 8-byte address) is placed on the stack.

  2. Person constructor is called.

As you can see, option #2 is generally more efficient.

The compilation error is probably the result of something like Person x("abc"):

The compiler first searches for a Person constructor which takes the explicit type of the argument that you are passing. In this case, it is Person(const char[]), which you have not defined.

Then, the compiler searches for any other Person constructor which takes a "convertable" type. Since operator const string&(const char[]) exists, the compiler can convert the argument "abc" from const char[] to const string&. So if you define Person(const string& name), then your code is successfully compiled.

Finally, the compiler explains for each Person constructor that you've defined, why it is not a suitable candidate for your call. In this case, it tells you that there is no operator string&(const char[]), which allows it to convert the argument "abc" from const char[] to string& before calling your Person(string&) constructor.

0
votes

&name is a reference which means it is just another name for the string you pass in as an argument. This allows you to pass in the object itself, not just its value. You have to use const if you want to pass in a const. A const cannot be modified.

0
votes

&name is a reference to name, so the actual object is passed as a parameter, not a copy. When you put const string &name, name is still being passed as a reference, but the const makes it so you cannot alter the actual object, just use it.

0
votes

The difference between const string &name and string name is that, const string &name is constant reference to a string, here another copy is not created when you pass it to the function , also it cannot be changed inside the function as it is constant.