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?
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 =
.
Question 4: Does it makes sense to generate the default implementation for only one of them when the other could not be generated?
Answer to the 3 Question seems to answer this.
Just to reiterate the operations being performed in your code example:
B b(a);
invokes the conversion copy constructor B::B(A&)
B b1 = b;
invokes the default copy constructor B::B(const B&)
Consider the additional scenarios.
If you had B b1 = a;
it would call B::B(A&)
and hence no error again.
But compiler would mark a error if B::B(A&)
was declared explicit
and would not be allowed for any implicit conversions
by acting as a conversion function
.
Check the same here.
Constructors in C++ language perform initialization, while assignment operators perform assignment. Initialization and assignment are tow completely different concepts.
References in C++ language can be initialized, which is why the compiler has no problem generating implicit copy constructors for classes with references. The B b1 = b;
statement uses that implicitly generated copy constructor. I don't see why you expect it to produce an error.
However, references themselves cannot be assigned (reassigned), which is why the compiler refuses to generate implicit copy assignment operators for classes with references. The compiler informed you about that by issuing a warning. If you actually try to use the assignment operator for class B
in your program, you'll end up with an error.
In this respect the situation with references in pretty much the same as situation with const
members: if some class has const
members, the compiler will have no problem generating the implicit copy constructor for this class, but will refuse to generate the implicit assignment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With