+1 But the doesn't necessarily have to be an abstract class. It's often the case though.
– ralphtheninjaJun 29 '09 at 8:59

4

Isn't it sufficient to declare a function to be pure virtual for defining a base class? Or the above is in absence of pure virtual function. What is a creation event for a Derived class of such an Abstract class?
– Amol GawaiJun 29 '09 at 12:41

5

Neil, I am not waging a language war here, just answered what protected ctor is good for. But you should be able to appreciate that there is a design-level concept of abstract class, and that it differs from the C++/Delphi definition.
– Henk HoltermanJun 29 '09 at 13:43

8

The canonical way to indicate a class is abstract is to make the destructor pure virtual. But I tend to make the constructors protected as well, both for "belt and suspenders" protection, and to make it clear to clients they cannot directly instantiate an object of the class.
– JohnMcGJun 29 '09 at 13:58

2

Combining with answers and comments, this answer is good for me to accept.
– Amol GawaiJun 30 '09 at 5:45

Non-public constructors are useful when there are construction requirements that cannot be guaranteed solely by the constructor. For instance, if an initialization method needs to be called right after the constructor, or if the object needs to register itself with some container/manager object, this must be done outside the constructor. By limiting access to the constructor and providing only a factory method, you can ensure that any instance a user receives will fulfill all of its guarantees. This is also commonly used to implement a Singleton, which is really just another guarantee the class makes (that there will only be a single instance).

The reason for making the constructor protected, rather than private, is the same as for making any other method or field protected instead of private: so that it can be inherited by children. Perhaps you want a public, non-virtual factory method in the base class, which returns references to instances of the derived classes; the derived classes obviously want access to the parent constructors, but you still don't want to be creating them outside of your factory.

A protected constructor means that only derived members can construct instances of the class (and derived instances) using that constructor. This sounds a bit chicken-and-egg, but is sometimes useful when implementing class factories.

...and a use of a friend class calling the protected constructor would be in the case of an object that has members that are constant (set by the constructor) but need to be public, but never to be set by any other public access, guarantees that the object wont be created somewhere else and the data therefore would not be modified anywhere else either.
– osirisgothraMar 2 '14 at 17:37

This creates instances of the class and guarantees that each of them has a unique incrementing integer id. Note that if the constructor you want to use is not the default, you must hide the default too.