Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

c++ - Assignment operator and copy constructor in the presence of references

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...