Daniel Mernyei Daniel Mernyei - 1 year ago 118
C++ Question

Convert char* to string&

I have the following code:

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
string &s1 = argv[0]; // error
const string &s2 = argv[0]; // ok

argv[0] = "abc";
cout << argv[0] << endl; // prints "abc"
cout << s2 << endl; // prints program name

I receive the following error for

invalid initialization of reference of type 'std::string& {aka std::basic_string<char>&}' from expression of type 'char*'

Then why does the compiler accept

Interestingly, when I assign a new value to
does not change. Does the compiler ignore that it's a reference and copies the value anyway? Why does it happen?

I write this in Code::Blocks 16.01 (mingw). The same happens with/without

Answer Source

Then why does the compiler accept s2?

Because a constant reference can bind to a temporary, and std::string has a constructor that can take a char * for a parameter.

That assignment constructs a so-called "temporary" object, and binds a constant reference to it.

Interestingly, when I assign a new value to argv[0] then s2 does not change.

Why should it change? s2 is a separate object. A std::string object does not maintain a pointer to a char * that created it. There are many ways to create a std::string. The actual string is owned by the object. If the std::string is constructed from a literal character string, it gets copied into the std::string. So, if the literal character string comes from a buffer, and the buffer is subsequently modified, it has no effect on the std::string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download