0

What happens when I make a assignment to object which has copy constructor but no assignment operator?
Will it call compiler's assignment operator, performing memberwise copy?

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52

4 Answers4

2

All classes have an assignment operator, unless you explicitly delete it (not possible prior to C++11). If you do not supply your own implementation, the compiler will supply one for you.

That is the main reason behind the rule of three: if you have a copy constructor, it is nearly certain that you will need an assignment operator and a destructor as well.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Yes you'll be accessing the default assignment operator generator by compiler, if you don't provide one.

But in general if a class defines one of the following it should probably explicitly define all three

  • destructor
  • copy constructor
  • copy assignment operator
P0W
  • 46,614
  • 9
  • 72
  • 119
1

The copy constructor plays no role in assignment, the default assignment operator will be called that'll do a bit-wise copy of built-in type members and call assignment operator on object members of class type.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • but if you do assignment like this `ClassName a; ClassName b = a;` a copy constructor will be called – Kastaneda Sep 28 '14 at 14:27
  • @Kastaneda that's not an assignment, that's a copy construction. Just because the syntax uses the `=` token doesn't make it an assignment. – Paul Evans Sep 28 '14 at 17:10
  • Yes, technically you're right. I have written it because (for example in Russian forums) people, that begin study C++, are often confused about the differences among these concepts. – Kastaneda Sep 28 '14 at 17:59
  • @Kastaneda you're wrong (and you're technically wrong because we're discussing technical issues) calling it assignment: "if you do assignment like this", it's (technically) a *copy initialization* which will use the copy constructor. – Paul Evans Sep 28 '14 at 18:21
  • Yes, i know. It is a question of terminology and i just study English and can not exactly expressed. – Kastaneda Sep 28 '14 at 18:33
  • @Kastaneda The C++ Standards define this *assignment*, *construction* and *initialization* terminology as it should be expressed. – Paul Evans Sep 28 '14 at 19:06
0

I don't know what do you mean by assignment ( in compiler's context ). So, Let me try by an example.Let's say we have a class Test;

Test a,c;          //default constructor would be called for both.

Test b = a;        //copy constructor would be called for b as we are creating that object.

c = b;             //assignment operator would be called for c as we are changing content's of c.

So, if class Test contains plain objects then it wouldn't matter if you define OR not compiler would do bit-wise copying for you. But if your class contains pointers, then you should explicitly define your copy constructor, assignment operator and destructor.

Hope I'm clear enough.

ravi
  • 10,994
  • 1
  • 18
  • 36