50

I decided to see if assigning a reference to a member would make a member a reference. I wrote the following snippet to test it. There's a simple class Wrapper with an std::string as a member variable. I take take a const string& in the constructor and assign it to the public member variable. Later in the main() method I modify the member variable but the string I passed to the constructor remains unchanged, how come? I think in Java the variable would have changed, why not in this code snippet? How exactly do references work in this case?

#include <iostream>
#include <string>
using namespace std;

class Wrapper
{
public:
   string str;

   Wrapper(const string& newStr)
   {
      str = newStr;
   }
};

int main (int argc, char * const argv[]) 
{
   string str = "hello";
   cout << str << endl;
   Wrapper wrapper(str);
   wrapper.str[0] = 'j'; // should change 'hello' to 'jello'
   cout << str << endl;
}
3
  • 5
    Types in C++ are what you decree, and they don't magically become something else. You say string, and you get a string. If you wanted a reference, you'd have said string &.
    – Kerrek SB
    Commented Feb 29, 2012 at 21:38
  • 2
    You abused people with too simple question :)
    – Mikhail
    Commented Feb 29, 2012 at 21:39
  • 2
    In Java the variable wouldn't have changed because the code wouldn't even have compiled; strings in Java aren't allowed to be modified. Commented Feb 29, 2012 at 21:40

6 Answers 6

51

To assign a reference in a constructor you need to have a reference member

 class A{
     std::string& str;
 public:
     A(std::string& str_)
     :    str(str_) {} 
 };

str is now a reference to the value you passed in. Same applies for const refs

 class A{
     const std::string& str;
 public:
     A(const std::string& str_)
     :    str(str_) {} 
 };

However don't forget that once a reference has been assigned it can not be changed so if assignment requires a change to str then it will have to be a pointer instead.

1
13

Because Wrapper::str is not a reference, it's an independent object. So when you do str = newStr, you're copying the string.

6
class Wrapper
{
public:
   string& str;

   Wrapper(string& newStr) : str(newStr) {}
};

Note, you cannot accept a const string& and store it in a string&, you would lose const-correctness in doing so.

6

You need to use an initializer and declare str as a reference as in:

class Wrapper {
public:
   string &str;

   Wrapper(string& newStr)
      : str(newStr) {
   }
};

The way you're writing it, all you are doing is copying the value of the reference you pass to the constuctor. You're not saving the reference. By declaring a reference as a class member and initializing it with a reference to another string instance, you will get the behavior you're looking for.

2
  • 1
    Your code tries to initialize a reference-to-mutable with a reference-to-const. Commented Feb 29, 2012 at 22:19
  • Fixed... that's what I get for blindly copying from the OP.
    – andand
    Commented Feb 29, 2012 at 22:31
1

You should declare Wrapper::str as a string&, not as a string.

1

Your main body variable is std::string.

Your parameter variable is const std::string&.

The const in references are always "low level const", meaning it modifies the type of object not the actual object.

In contrast a "top level const" modifies an actual object. Read C++ Primer on Top level const for clarification.

Here is how your assignment looks like when you pass arguments:

const std::string& = std::str; //Values are ommited
// i.e const std::string newStr = std::string str

You are initializing a const type reference with a non-const value which is acceptable. You are not supposed to change the value of std::string str using that reference. And, if you try changing the value of newStr inside the constructor, you will get a compilation error.

Next you're doing another assignment inside the constructor which is also acceptable:

std::string = const std::string 

The fact that wrap.str[0] didn't change str of main is that, although a reference was used to instantiate class str, class str has its own object and is not linked to main str. Using that reference in a parameter just links that parameter to main str; not main str to class str.

If your class variables were referenced, then it could have changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.