How instance creation of base class of existing code can be stopped while modifying the design?
class base {};
class der1 : public base{};
class der2 : public base{};
..........
..........
clas开发者_运维知识库s derXXX : public base{};
Several class have been derived from base. existing code: base class is not abstract class, so there are instance of base class alongwith all derive class.All the testing have been done.
Requirement:Now I want to stop instance creation of base class without any changing /testing of all derive class. How it can be done?
One option would be tom make every constructor of Base protected, so they can only be accessed by derived classes and the class itself. This would prevent instantiation of the object by itself but permit the instantiation of base classes.
If the base class does not have a constructor, then just declare a new, empty constructor that takes no arguments. Also be sure to provide an implementation of the copy constructor (or leave it unimplemented and private) so that the automatically-generated copy constructor is not left publicly visible.
Make all of the base
class constructors (including the default constructor and copy constructor) private or protected.
精彩评论