0

Possible Duplicate:
What is The Rule of Three?

Why is it recommended to provide implementation of copy constructor instead of using compiler provided "default copy constructor" ?

Community
  • 1
  • 1
yuvaraj
  • 1
  • 1

2 Answers2

0

If your class contains pointer members, which are dynamically allocated then you need to provide your own version of copy construcor because the default version just makes a shallow copy of them.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • This rule is too broad and generally just false for any classes that I implement that have pointers. Pretty much the only reason I ever use pointers is as non-owning re-assignable references. And in that case, the compiler generated copy constructor is fine. – Benjamin Lindley Jul 25 '11 at 04:53
  • @Benjamin Lindley: Hope that helps understand better. – Alok Save Jul 25 '11 at 05:05
0

It's not.

The default copy constructor is perfect in 99.9% of cases.

The exception of classes with owned pointers. Here the shallow copy of the default copy constructor does not work as expected for beginners.

But then you should never have pointers in your class so it becomes a non issue. To make this clear any owned pointers should be contained in a smart pointer (or container type) object. So it is a non issue.

If you are writing a smart pointer or container like object, then you need to implement the rule of three.

Martin York
  • 257,169
  • 86
  • 333
  • 562