0

I read here that if I don't write a copy constructor the compiler does it for me using the assignment operator, which results in shallow copy of Objects. What if I do have the assignment operator overloaded in all of my member object? wouldn't it result in a deep copy?

Cœur
  • 32,421
  • 21
  • 173
  • 232
yotamoo
  • 4,974
  • 11
  • 41
  • 60

3 Answers3

3

if I don't write a copy constructor the compiler does it for me using the assignment operator

No, it doesn't use the assignment operator; it does it via a implicitly generated copy constructor which does a shallow copy.

What if I do have the assignment operator overloaded in all of my member object? wouldn't it result in a deep copy?

Given that the assignment operator is not used in absence of explicitly defined copy constructor, even though you have the assignment operator overloaded you still need to overload the copy constructor as well.

Read the Rule of Three in C++03 & Rule of Five in C++11.

Community
  • 1
  • 1
Alok Save
  • 190,255
  • 43
  • 403
  • 518
  • These *Rules of N* should also come with a warning, "don't write any of them (unless you have a *really* good reason to). – Kerrek SB Sep 21 '11 at 19:39
  • @Kerrek: And then another one that says "ignore Kerrek SB" – Lightness Races in Orbit Sep 21 '11 at 19:43
  • @KerrekSB: Maybe, Just say, Don't use pointer members unless you have a very good reason to. :) – Alok Save Sep 21 '11 at 19:43
  • 1
    @Tomalak: If you know your tools, you'll do fine, of course. But many times the reasoning is, "C++ is hard. Oh, now I know classes. Oh, now I can put my `char *` in a vector. Now how do I write the destructor." etc. Point is, with well composed class design, writing any of the Big 3/5 should be a rare, localised occurrence. – Kerrek SB Sep 21 '11 at 19:47
  • @Kerrek: Sure, but we have to assume that the programmer _does_ have a good reason, because otherwise they would not have to read the Rules in the first place. This is the fail-safe approach. Those of us who know what we are doing are free to ignore the rules that we know we don't need. – Lightness Races in Orbit Sep 21 '11 at 19:52
0

The important part in the linked article is "each member of the class individually using the assignment operator." So it doesn't matter if you define the assignment operator for you class, it will use the assignment operator for each member of your class.

IronMensan
  • 6,520
  • 1
  • 24
  • 35
0

You're misinformed. The implicitly generated constructors and assignment operators simply perform construction or assignment recursively on all members and subobjects:

  • copy constructor copies element by element

  • move constructor moves element by element

  • copy assignment assigns element by element

  • move assign move-assigns element by element

This logic is the reason why the best design is one in which you don't write any copy constructor (or any of the other three, or the destructor) yourself, and instead compose your class of well-chosen, single-responsibility classes whose own semantics take care of everything.

Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
  • A *nitpick* The Q is not tagged C++11 actually. – Alok Save Sep 21 '11 at 19:40
  • 1
    @Als: I could say that the question isn't tagged C++03 :-) – Kerrek SB Sep 21 '11 at 19:40
  • But then being *pedantic* C++11 has a separate tag here, So C++03 is the only one being referred here by C++ tag, it doesn't have a separate tag. ;-) – Alok Save Sep 21 '11 at 19:48
  • @Als: interestingly, there's neither a C++98 nor a C++03 tag. Perhaps a C++98/03 tag should be inceived for the purpose of dedicated questions pertaining to that version of the standard. – Kerrek SB Sep 21 '11 at 19:51
  • @Kerrek: At some point soon we're going to have to decide which standard `c++` defaults to. It's already starting to get a bit messy with all the `c++/c++11` posts. Maybe we rename `c++` to `c++03` (for backwards-compatibility) and keep revision-specific ones. But then not all questions are revision-specific. Argh! – Lightness Races in Orbit Sep 21 '11 at 19:54
  • 1
    Actually I suppose we can just clarify versions as and when required, like we do for any other versioning. e.g. "I'm using jQuery 1.5.2" – Lightness Races in Orbit Sep 21 '11 at 19:55
  • 1
    @Tomalak: That's what I figured. For anything that's sufficiently version-independent, the "c++" tag is just fine. "c++11" should be only for explicitly new stuff, while the purported "c++98/03" tag could be for things that explicitly do *not* want to use any new features. – Kerrek SB Sep 21 '11 at 20:30