0

I am new to c++. I was told that invoking a constructor internally or externally are same. Why is assignment operation not involved in the case of invoking constructor explicitly?

Object A(3)                //implicit
Object A = Object(3);      //explicit

I think an object will get created when we do Object(3); So how are these two things turn out to be same?

starkk92
  • 5,754
  • 9
  • 43
  • 59
  • Because although it uses the assignment operator, it's not an assignment. It's a copy constructor call (which is allowed to be optimised away, and line 2 will likely do the exact same thing as line 1). – Simple Jul 03 '14 at 10:28
  • The two are semantically different, are likely to do exactly the same, and should have the same result regardless. – juanchopanza Jul 03 '14 at 10:31
  • These are both explicit invoking of the constructor. – M.M Jul 03 '14 at 10:50

1 Answers1

0

Take a look at this answer for more on the subject, but assignment operator is invoked when you are dealing with an already existing object. You'll notice similar behaviour for copy constructors:

Object A;
// the following two lines will call the copy constructor 
// even if the assignment operator is defined.
Object B = A;
Object C(A);
Community
  • 1
  • 1
Nasser Al-Shawwa
  • 3,573
  • 17
  • 27