Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should copy constructor be private or public

I am writing an abstract class that will be a parent for several other classes. I am thinking the copy constructor should be made private because you are using an abstract class and there is nothing to copy. However, I am not a 100% sure.

Am I correct and if I am not why should it be public or protected?

like image 347
Aaron Avatar asked Feb 23 '12 16:02

Aaron


2 Answers

The copy constructor should be private if you do not want objects of the class to be copied. Otherwise, it should be public.

like image 128
BЈовић Avatar answered Sep 21 '22 03:09

BЈовић


I think protected is the best choice: it leaves the decision on whether or not the object is copyable to the derived classes, while prohibiting the copying at the abstract class level, preventing the dreaded object slicing.

like image 27
Sergey Kalinichenko Avatar answered Sep 20 '22 03:09

Sergey Kalinichenko