4

I've read in C++ primer plus thats says

implementations have the option of handling this statement in two steps: using the copy constructor to create a temporary object and then using assignment to copy the values to the new object.That is, initialization always invokes a copy constructor, and forms using the = operator may also invoke an assignment operator

And I've also read on some websites that says code like A a2 = a1; is the same as A a2(a1);, which means A a2 = a1 only invokes the copy constructor.

So my question is that when the program uses only copy constructor and when it uses both copy constructor and assignment operator. And who decides that, is it compiler?

5
  • 1
    With A a2 = a1; you don't have assignment (despite the use of the =). It's initialization (and plain copy-construction). With A a1, a2; a2 = a1; you have assignment. Commented Oct 19, 2018 at 18:01
  • Read the rules in the standard about "copy initialization". Commented Oct 19, 2018 at 18:02
  • 6
    Initialisation never uses the assignment operator, and C++ Primer Plus is a terrible learning resource. Commented Oct 19, 2018 at 18:05
  • 7
    Better books. Commented Oct 19, 2018 at 18:06
  • Why Copy Constructor is called here instead of normal Constructor and overloaded assignment operator?. Commented Oct 19, 2018 at 18:06

1 Answer 1

6

C++ initialization statements like

A a2 = a1;

never use the overloaded assignment operator.

Only statements like

A a2; // Default construction
a2 = a1; // Applying assignment

would ever use the assignment operator.

The difference is, that if you see = in the same line as the variable definition is done (with the type in front), that's considered as initialization by the compiler, and not assignment.

Copy constructor and assignment operator never would be used at the same time.

implementations have the option of handling this statement in two steps: using the copy constructor to create a temporary object and then using assignment to copy the values to the new object.That is, initialization always invokes a copy constructor, and forms using the = operator may also invoke an assignment operator

Your book is probably wrong about this (at least I never noticed any compiler implementation working this way), or you misinterpreted that statement from the context.

Sign up to request clarification or add additional context in comments.

7 Comments

I'm curious about what happens in an ephemeral case like A a2 = *(new A(a1))?
@JohnCvelth How do you think that's relevant? There's still no assignment done with your example.
"The same line" has nothing to do with it. C++ doesn't care what kind of whitespace is in your source code. A a2; a2 = a1; is assignment despite being a single line.
@JohnCvelth There's no move initialization in question anyways.
@JohnCvelth new A(a1) directly initializes the A new creates with a copy of a1. Since dereferencing it gives you an lvalue you then copy initialize a2 with that object you just made. So, two calls to the copy constructor and a memory leak is what you get.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.