Assignment operator and copy constructor in the presence of references
Asked Answered
C

5

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?

Conidiophore answered 25/5, 2011 at 6:8 Comment(4)
Your code doesn't use assignment operator anywhere, why does it give that warning? By the way, GCC 4.5.0 doesn't give any warning even with -Wall -pedantic.Fanjet
MSVC is stupid and warns everytime it cannot generate operator= for a class (because the class contains a reference or a constant member). GCC waits till you actually try to use the operator.Aggression
Far from stupid, this encourages you to keep your classes clean. That's what warnings, as opposed to errors, are for. If you don't get the warning simply because you don't perform an assignment, all you do is defer the problem to the next guy who might want to—and who will be far less willing and able to fix your code.Jumpoff
(5 minute editing period passed) Your right course of action is to either implement an assignment operator yourself, or explicitly prohibit assignments (not ignoring the warning, if that's what you're tempted to do ;)). That way, whoever might want to use your code down the line will know what's up.Jumpoff
R
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 =.

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.

Roanne answered 25/5, 2011 at 6:18 Comment(1)
Again, the term "default" has a specific meaning in C++ language. There's no such thing as "default copy constructor". There is "default constructor" and "copy constructor". In this case the copy constructor is implictly declared and implictly defined by the compiler.Dilation
D
4

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.

Dilation answered 25/5, 2011 at 16:52 Comment(3)
AFAIU there is no assignment operation in Op's example, the two cases are copy initialization & direct initialization, basically both will invoke calls to constructors, I do not understand where the argument of assignment comes from, can you please elaborate?Roanne
@Als: You are right, there's no assignment there. I never said there is. This is why the compiler issues a mere warning, for purely informational purposes. If the program attempted to actually use assignment, the compiler would have generated an error, not a warning.Dilation
True that the compiler won't generate the implicit bit wise assignment if the class has a reference, but that particular warning is completely unrelated to the the operations of default initialization & copy initialization in OP's question. The OP seems to have confused the warning as emanating due to those operations and OP's Q's seems to imply that these two are related bu they are not.Roanne
N
1

References can only be initialized one time, and cannot be changed. The constructor is valid because it initializes m_a, but the copy would reassign the m_a, which is forbidden.

Nanci answered 25/5, 2011 at 6:13 Comment(3)
c = b; /* forbidden */ No. It is not forbidden. It just means different thing - assigning value of b to a via reference c.Terse
You are right. My example is not good at all. I'll remove it.Nanci
"My example is not good at all." Actually I think your code was a good illustration why exactly assignment of reference is not possible. Only comment was not correct.Terse
H
1

Your sample doesn't need an assignment operator. VC++ is simply warning you that it wouldn't be able to generate one if it needed to. That can be useful information if you're writing a library and you forgot to anticipate that the user of your library might need to copy B's.

If you don't want the warning, you can suppress it (e.g., with a #pragma), or your can declare a private assignment operator and not implement it. If somebody then tries to invoke the assignment operator, it'll fail with a link-time error.

Houseboat answered 25/5, 2011 at 17:29 Comment(0)
G
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&).)

Gearbox answered 25/5, 2011 at 6:13 Comment(2)
Sorry that was a mistake in my question.. I meant the statement B b1=b;.Conidiophore
The term "default" has a specific meaning in C++ language. There's no such thing as "default copy constructor". There is "default constructor" and "copy constructor". In this case the copy constructor is implictly declared and implictly defined by the compiler.Dilation

© 2022 - 2024 — McMap. All rights reserved.