operator= for a class with a const reference member

B

Bart Simpson

I have a class that has a member that is a const reference:

class MyClass
{
public:
MyClass(const AnotherClass& ac);
MyClass(const MyClass& mc);
MyClass& operator= (const MyClass& mc);

private:
const AnotherClass &m_ref ;
};


How do I implement the assignment "constructor"?

MyClass& MyClass::eek:perator= (const MyClass& mc)
{
m_ref = mc.m_ref ; //dosen't compile (obviously)
m_ref(mc.m_ref) ; //dosen't compile (obviously)
}
 
V

Victor Bazarov

Bart said:
I have a class that has a member that is a const reference:

class MyClass
{
public:
MyClass(const AnotherClass& ac);
MyClass(const MyClass& mc);
MyClass& operator= (const MyClass& mc);

private:
const AnotherClass &m_ref ;
};


How do I implement the assignment "constructor"?

MyClass& MyClass::eek:perator= (const MyClass& mc)
{
m_ref = mc.m_ref ; //dosen't compile (obviously)
m_ref(mc.m_ref) ; //dosen't compile (obviously)
}

What you're trying to do here is to re-seat the reference. It is
impossible (without some dirty tricks). Once constructed, the object
cannot be assigned because it contains a reference. If you cannot do
without assigning to such objects, they shouldn't contain references
and instead should contain pointers.

V
 
H

Howard

Victor Bazarov said:
What you're trying to do here is to re-seat the reference. It is
impossible (without some dirty tricks). Once constructed, the object
cannot be assigned because it contains a reference. If you cannot do
without assigning to such objects, they shouldn't contain references
and instead should contain pointers.

Also, it doesn't matter whether the member reference is const or not. You
can't change a non-const reference, either - without the aforementioned
dirty tricks, at least.

-Howard
 
Z

Zeppe

Victor said:
What you're trying to do here is to re-seat the reference. It is
impossible (without some dirty tricks). Once constructed, the object
cannot be assigned because it contains a reference. If you cannot do
without assigning to such objects, they shouldn't contain references
and instead should contain pointers.

Lol, that was actually the question I answered in the previous thread of
this poster. The funny thing is, I answered one hour before the actual
question was sent :)

Regards,

Zeppe
 
F

Fei Liu

Bart said:
I have a class that has a member that is a const reference:

class MyClass
{
public:
MyClass(const AnotherClass& ac);
MyClass(const MyClass& mc);
MyClass& operator= (const MyClass& mc);

private:
const AnotherClass &m_ref ;
};


How do I implement the assignment "constructor"?

MyClass& MyClass::eek:perator= (const MyClass& mc)
{
m_ref = mc.m_ref ; //dosen't compile (obviously)
m_ref(mc.m_ref) ; //dosen't compile (obviously)
}

Here:
http://meditation-art.blogspot.com/2007/04/reference-type-cannot-be-reseated-in-c.html

It's generally a poor idea to have reference type class member if you
intend to use it with STL containers and it's a even poorer idea to have
a const reference type class member.

F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top