Why const reference string

Hi everyone,
I just wrote this piece of code to test something, but it didn't go through the GCC-compiler because of the following error:

C:\Project\TESTS\QtCreator_Pro\Constructers\main.cpp:22: ERROR: invalid initialization of non-const reference of type 'std::__cxx11::string& {aka std::__cxx11::basic_string<char>&}' from an rvalue of type 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}'
Person pers{"Manu", 2500};




class Person{
public:
Person(std::string &n, double sal): name{n}, salary{sal}
{

}
private:
std::string name;
double salary;
};


int main()
{
Person pers{"Manu", 2500};
return 0;
}


Please consider the first parameter of the constructor. It is a non-const. the code would not work unless I turn this parameter to const reference: Person(const std::string &n, double sal) or
I declare it just as a value, not as a reference: Person(std::string n, double sal).
But why ist that so? although readed the error-massage it is still not obvious to me why I need a const reference to string in my constructor in order to make my object like shown in the main function: Person pers{"Manu", 2500};
https://19216801.win/ https://routerlogin.cloud/ https://192168101.red/
can anyone teach me about this issue or about constructors, references, or strings in this particular contex?

Thanks a lot.
Last edited on
The reason is that you're passing "Manu" which is a string literal and can't be changed. Passing by ref (string &n) requires that the called data be changeable. Using const string &n removes the requirement that it can be changed - as it's specified const.

string n works because this is passing-by-value so that n is a copy of the passed data - and any changes made would just affect the copy.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

class Person{
public:
    Person(std::string& n, double sal): name{n}, salary{sal}
    {
        
    }
private:
    std::string name;
    double salary;
};


int main()
{
    std::string nm{"Manu"};
    Person pers(nm, 2500);
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

class Person
{
public:
   Person( const std::string &n, double sal) : name{n}, salary{sal} { std::cout << name << " " << salary << '\n'; }
private:
   std::string name;
   double salary;
};


int main()
{
   Person pers{"Manu", 2500};
}


You are sending the constructor "Manu", which is of type const char* and can't be changed (immutable).
You are trying to receive it in something which is of type std::string&, which you could (in principle) change; i.e. it is mutable.

Either send it a mutable string (as @Againtry), or add the qualifier const in the constructor, or just remove the reference &.
Last edited on
The const reference is special. The compiler is free to generate a temporary object or take the reference. When you pass an object that cannot be used as a reference (i.e. it is not exactly the type std::string) the compiler is looking for a suitable constructor (that allows implicit conversion) and generates a temporary obect by calling that constructor.

So for
1
2
3
4
int main()
{
   Person pers{"Manu", 2500};
}
it is basically as if you have a constructor like Person(std::string n, double sal): name{n}, salary{sal}

If you had
1
2
3
4
5
int main()
{
  std::string x = "Manu";
   Person pers{x, 2500};
}
x is passed as the refence to a const std::string. No temporary variable is generated
Topic archived. No new replies allowed.