Passing by reference to a constructor

57,150

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

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

Share:
57,150
rcplusplus
Author by

rcplusplus

Updated on February 23, 2020

Comments

  • rcplusplus
    rcplusplus over 4 years

    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;
    }
    
  • Geekoder
    Geekoder over 12 years
    Your code tries to initialize a reference-to-mutable with a reference-to-const.
  • andand
    andand over 12 years
    Fixed... that's what I get for blindly copying from the OP.
  • Markon
    Markon over 2 years
    Interesting to know that having references as data members is something to avoid: herbsutter.com/2020/02/23/references-simply