-4

Why is it recommended that you explicitly declare a copy constructor,even when the compiler makes a public copy constructor when you use objects as parameters , use objects as a return value or even construct an object based on another of the same class?

C3X0r
  • 1
  • 1
  • Can you give an example? – alfC Jun 24 '19 at 07:45
  • 1
    If the _object_ contains any _pointers_ as data member then _default or shallow copy constructor_ won't work, In that case you need to define _copy constructor_ explicitly. – Achal Jun 24 '19 at 07:51
  • 3
    "Never" is a pretty good answer (if a class "needs" a copy constructor then you can redesign it in terms of standard containers that already have correct copy semantics) – M.M Jun 24 '19 at 07:52
  • 1
    I don't think it is recommended. – melpomene Jun 24 '19 at 07:52
  • 2
    @M.M That is a very strong statement. You can easily find yourself writing a resource-handling class or container which cannot easily be expressed using existing `std` classes. Of course, such a class should *only* do the resource handling, and leave higher logic to classes which will aggregate it. – Angew is no longer proud of SO Jun 24 '19 at 08:02
  • @Angew It's a generalization, there will be some exceptions as you say... many resource-handling containers can be `unique_ptr` with custom deleter – M.M Jun 24 '19 at 08:08
  • 3
    Are you sure that you don't mix up "explicitly declare a copy c'tor' and "declare copy c'tor explicit (if you implemennted one)" ? that makes a big difference. Have a look at the "explicit" keyword – choosyg Jun 24 '19 at 08:16
  • @choosyg Don't think I've _ever_ seen an `explicit` copy ctor nor can I imagine why the OP would be asking about that – Lightness Races in Orbit Jun 25 '19 at 11:04
  • @LightnessRacesinOrbit To make object not implicit copyable, see https://stackoverflow.com/questions/11480545/explicit-copy-constructor – choosyg Jun 25 '19 at 11:42
  • @choosyg I know what it does, I'm saying I don't think I've ever seen anyone use it and there's no evidence that the OP wants to – Lightness Races in Orbit Jun 25 '19 at 11:44

2 Answers2

0

Copy constructor is needed when object has dynamic memory allocations. In default c++ compiler creates copy constructor, so when you do not have pointer etc. you do not need to define copy constructor.

0

Why is it recommended that you explicitly declare a copy constructor,even when the compiler makes a public copy constructor

It isn't.

If you don't have special logic to perform in a copy constructor, you don't need to provide one, and doing so is just noise.

If you do, then obviously you have to write that code by providing your own copy constructor.


Some older texts might propose you declare all special member functions, even if their definitions are empty, because if you want to add meaningful definitions later you then do not change the header that contains the definition for the class. This helps to reduce rebuild times and compatibility issues with projects using your code.

But if this ever happened it would almost certainly because you modified or added some data members, so you'd have to modify the header anyway. And, frankly, such a substantial change to a class's semantics warrants a bit of a careful eye anyway. I don't see the benefit in making code as verbose as possible just for the sake of making changes that shouldn't be completely transparent, transparent.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055