title: passing string by reference and updating its value through function argument in C++
body: <p>So if i have a function like:</p>
  
  <pre><code>void edit(string&amp; s) {
      //do something with s
  }
  </code></pre>
  
  <p>and i use it like:</p>
  
  <pre><code>std::string str = "Hello";
  edit(str);
  </code></pre>
  
  <p>This will update the value of <code>str</code>.</p>
  
  <p>But now i have a class, in which header file has a construtor like:</p>
  
  <pre><code>class MyClass {
  
     private:
        //something
     protected:
        std::string&amp; str;
     public:
        //something
  
     MyClass(std::string&amp; s): str(s){}
  };
  </code></pre>
  
  <p>and later in the source file, i calculate a string which i want to update through the string argument that was passed to the constructor, but it does not work.</p>
  
  <p>Can you please give me some idea how can i make that work, such that the class variable that i assign a <code>std::string</code> value, is reflected in the argument string which was passed to the constructor of this class.</p>
  
  <p>Thank you,</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>With the above change as suggested (making str a reference), i get this error:</p>
  
  <p>"error: str must be initialized in cosntructor base/member initializer list"</p>
  
  <p><strong>UPDATE- SOLUTION:</strong></p>
  
  <p>Hi, so following this approach i seem to have found the solution:</p>
  
  <pre><code>  class MyClass {
  
         private:
            //something
         protected:
            std::string* str;
         public:
            //something
  
         MyClass(std::string* s): str(s){}
      };
  </code></pre>
  
  <p>Later, i have:</p>
  
  <pre><code>string mystring;
  </code></pre>
  
  <p><code>MyClass(&amp;mystring);</code> and voilla it seems to wrok well.</p>