0

Edit: sorry I used "assignment constructor" instead of "assignment operator" in my original post. Fixed now.

It turns out that the copy constructor is called instead of the assignment operator in the following code. Anyone can tell me the reason behind this? Thank you.

class A

{
int i;
public:
A(int ii) { i = ii; }
A(const A& a) { i = a.i; i++; }
A& operator=(const A& a) { i = a.i; i--; }
};
int main(void)
{
A a(4);
A b = a;
return 0;
}
EXP0
  • 872
  • 2
  • 12
  • 20
  • 2
    Does that code even compile? You're not returning anything from `operator=` (which is _not_ a constructor). – Mat Aug 28 '11 at 20:54
  • it compiles fine in g++. – EXP0 Aug 28 '11 at 20:59
  • `warning: no return statement in function returning non-void`. Use `-Wall` and save yourself a **lot** of problems down the road. – Mat Aug 28 '11 at 21:01
  • It's also worth noting that in C++, int main() can be left without a return value at which point it defaults to returning 0. – Charles Beattie Aug 28 '11 at 21:33

2 Answers2

7
A a(4);
A b = a;

None of them is assignment1. Both are initialization.

First one is called direct-initialization, and second one is called copy-initialization.

The difference between them is that first one would work even if the copy-constructor is inaccessible (i.e its either private or protected), and second one would NOT work if the copy-constructor is inaccessible.

Even though the second one requires the copy-constructor to be accessible, that doesn't mean that the copy-constructor will necessarily be called. The compiler is allowed to optimize this, and so can elide the call to copy-constructor altogether. An accessible copy-constructor is needed for semantic validation.

See these topics :

1. And there is nothing such a thing called "assignment constructor".

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    Thank you very much for the detailed explanation. If both works, are they different in terms of efficiency? Thanks. – EXP0 Aug 28 '11 at 21:06
  • @EXP0: A good compiler would produce same machine-instructions for both. So there should *not* be any difference between them in terms of efficiency. – Nawaz Aug 28 '11 at 21:09
4

operator= is not an "assignment constructor", it is an "assignment operator".

When you initialize a variable in its definition (as in A b = a), it is by definition equivalent to calling the copy constructor. ie, A b(a); and A b = a; are exactly equivalent.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • 2
    Note: This is only true for any constructor not marked explicit. Any explicit constructor *cannot* be used with equals notation; you must construct it explicitly. – Nicol Bolas Aug 28 '11 at 20:54
  • @bdonlan - can you confirm, is this required by the C++ standard, or is this just an optimisation which some compilers implement (i.e. replacing default constructor followed by assignment operator with a single call to the copy constructor)? – AAT Aug 28 '11 at 20:58
  • This is required by the standard. You can do this even with a class that does not have a default constructor. – bdonlan Aug 28 '11 at 21:00