Alex Rivera | Logout

Assignment operator and copy constructor in the presence of references

Asked 2011-05-25T06:08:05.983
10

I am just experimenting with the references using this code:

class A
{
};

class B
{
public:
    B(A& a): m_a(a){}

    A& m_a;
};

int main()
{
    A a;
    B b(a);
    B b1 = b;
}

I was expecting both B b1 = b; to produce a error. Instead when I compile with VS2008 I just get a warning

warning C4512: 'B' : assignment operator could not be generated

I understand why I am getting this warning. But shouldn't the compiler generating an error for the B b1 = b; statement too? It is like it generated copy constructor but didn't generate assignment operator. Aren't these two inherently linked to one another ? does it makes sense to generate the default implementation for only one of them when the other could not be generated?

Edit
Report

2 Answers

11
warning C4512: 'B' : assignment operator could not be generated

Question 1: Why this warning?
References can only be initialized once when they are created. You cannot reassign a reference to another same type variable after creation because Reference is just an alias of the type variable for which it was created and will continue to remain so. Attempting to reassign it generates an error.
Usually, a compiler generates an implicit bit wise assignment operator every class by default but in this case Since the class B has an reference as an member m_a, If the compiler were to generate an implicit assignment operator it would break the fundamental rule that references cannot be reassigned. So the compiler generates this warning to inform you that it could not generate the implicit assignment operator.

Question 2: But shouldn't the compiler generating an error for the B b1 = b; statement too?
The generated warning and this particular operation have no relation at all.
B b1 = b; invokes the implicit(as rightly pointed out by @AndreyT) copy constructor B::B(const B&). Implicit copy constructor is one of the member functions which a class generates by default. So there is no warning or error for it.

Question 3: It is like it generated copy constructor but didn't generate assignment operator. Aren't these two inherently linked to one another ?
No they are not related at all. Yes compiler generated a copy constructor but it could not generate a assignment operator for the reason specified in answer to Question 1 above. This is because the member reference m_a could be initialized in the body of the constructor itself. it's just the initial assignment at time of creation not assignment as in case of =.

Questi

answered 2011-05-25T06:18:50.147
0

B b(a); is a valid statement. Because B::B(A&) is invoked when you pass the object of type A to the constructor of B.

By the way with g++ there is no warning generated as you mentioned. (And IMHO, there should not be any warning because B b1= b; invoked default copy constructor B::B(const B&).)

answered 2011-05-25T06:13:21.770

Your Answer