Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement operator=() for a class that has a reference member variable?
    primarykey
    data
    text
    <p>I want to have an object that contains a reference, and put that object into a vector...</p> <p>Must I use smart pointers instead of a member references in any object I want to push into a vector? This was what I wanted to do:</p> <pre><code>#include &lt;string&gt; #include &lt;vector&gt; using namespace std; class MyClass { public: MyClass(const string&amp; str_ref); //constructor MyClass(const MyClass&amp; mc); //copy constructor private: string&amp; my_str; }; MyClass::MyClass(const string&amp; str_ref) : my_str(str_ref) {} MyClass::MyClass(const MyClass&amp; mc) : my_str(mc.my_str) {} int main() { //create obj and pass in reference string s = "hello"; MyClass my_cls(s); //put into vector vector&lt;MyClass&gt; vec; vec.push_back(my_cls); return 0; } //Throws Error //ref.cpp:6:7: error: non-static reference member ‘std::string&amp; MyClass::my_str’, can’t use default assignment operator </code></pre> <p>However it says I need to implement my own operator=() as the default generated one isn't valid but of course, there is no legal way to do so...</p> <pre><code>#include &lt;string&gt; #include &lt;vector&gt; using namespace std; class MyClass { public: MyClass(const string&amp; str_ref); //constructor MyClass(const MyClass&amp; mc); //copy constructor MyClass operator=(const MyClass&amp; mc); //operator = private: string&amp; my_str; }; MyClass::MyClass(const string&amp; str_ref) : my_str(str_ref) {} MyClass::MyClass(const MyClass&amp; mc) : my_str(mc.my_str) {} //not a constructor. should not construct new object //and return that? MyClass MyClass::operator=(const MyClass&amp; mc) { if (this != &amp;mc) { //test for self-assignment. my_str(mc.my_str); //can't reseat refs. this shouldn't work. } return *this; } int main() { //create obj and pass in reference string s = "hello"; MyClass my_cls(s); //put into vector vector&lt;MyClass&gt; vec; vec.push_back(my_cls); return 0; } //THROWS: //ref2.cpp: In constructor ‘MyClass::MyClass(const string&amp;)’: //ref2.cpp:18:19: error: invalid initialization of reference of type ‘std::string&amp; {aka //std::basic_string&lt;char&gt;&amp;}’ from expression of type ‘const string {aka const //std::basic_string&lt;char&gt;}’ //ref2.cpp: In member function ‘MyClass MyClass::operator=(const MyClass&amp;)’: //ref2.cpp:29:18: error: no match for call to ‘(std::string {aka std::basic_string&lt;char&gt;}) //(std::string&amp;)’ </code></pre> <p>So am I forced to use a smart pointer here or anything other than a reference?</p> <p><strong>EDIT: This is a simplification. String&amp; is not the object being passed, it's a more complex object itself containing a vector object.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload