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?

taotsi
  • 354
  • 2
  • 11
  • 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*. – Some programmer dude Oct 19 '18 at 18:01
  • Read the rules in the standard about "copy *initialization*". – Jesper Juhl Oct 19 '18 at 18:02
  • 6
    Initialisation _never_ uses the assignment operator, and C++ Primer Plus is a terrible learning resource. –  Oct 19 '18 at 18:05
  • 7
    [Better books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jesper Juhl Oct 19 '18 at 18:06
  • [Why Copy Constructor is called here instead of normal Constructor and overloaded assignment operator?](https://stackoverflow.com/questions/10595451/why-copy-constructor-is-called-here-instead-of-normal-constructor-and-overloaded). – cpplearner Oct 19 '18 at 18:06

1 Answers1

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.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • I'm curious about what happens in an ephemeral case like `A a2 = *(new A(a1))`? – John Cvelth Oct 19 '18 at 18:12
  • 1
    @JohnCvelth How do you think that's relevant? There's still no assignment done with your example. – πάντα ῥεῖ Oct 19 '18 at 18:13
  • @JohnCvelth `type name = whatever` is **always** copy initialization – NathanOliver Oct 19 '18 at 18:20
  • 3
    "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. – Ben Voigt Oct 19 '18 at 18:20
  • @NathanOliver, I'm more curious about whether copy/move initialization happens once or twice in this(`A a2 = *(new A(a1))`) case. I know it's off-topic, but it still seems like rather appropriate place to ask. – John Cvelth Oct 19 '18 at 18:22
  • 1
    @JohnCvelth There's no move initialization in question anyways. – πάντα ῥεῖ Oct 19 '18 at 18:24
  • 1
    @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. – NathanOliver Oct 19 '18 at 18:30
  • @NathanOliver, Thank you. – John Cvelth Oct 19 '18 at 18:32