views:

1761

answers:

7

Why would anyone declare a constructor protected? I know that constructors are declared private for the purpose of not allowing their creation on stack.

+6  A: 

one use could be factory patterns

Umair Ahmed
+4  A: 

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.

anon
Technically, this applies only if ALL ctors are protected.
MSalters
+11  A: 

When a class is (intended as) an abstract class, a protected constructor is exactly right. In that situation you don't want objects to be instantiated from the class but only use it to inherit from.

There are other uses cases, like when a certain set of construction parameters should be limited to derived classes.

Henk Holterman
+1 But the doesn't necessarily have to be an abstract class. It's often the case though.
Magnus Skog
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 Gawai
@Henk Holterman It certainly does - see Section 10.4 of the C++ Standard, entitled "Abstract Classes"
anon
C++ only has an implicit concept of abstract class, based on the presence of abstract functions. If you have an architecturally abstract class w/o pure virtuals, there is no direct way of marking it abstract.
Henk Holterman
The presence of a PVF is "explicit", in any version of the English language I've ever come across.
anon
Neil, but what if I want an abstract class w/o a PVF? C# and Java have "abstract class X", that is what I call explicit.
Henk Holterman
So you want an unecessary "abstract" keyword? That's not the way C++ works. But it doesn't mean that there is no concept of an abstract class when the meaning of such a thing is EXPLICITLY described in the C++ standard itself!
anon
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 Holterman
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.
JohnMcG
Combining with answers and comments, this answer is good for me to accept.
Amol Gawai
+1  A: 

To let a subclass use a constructor that should not be accessible to an instantiator directly.

Oliver Hanappi
A: 

You could use it to limit the classes that could create it, for example:

class Level
{
private:

 Level();
 ¨Level();

 friend class LevelManager;
};

The only class that can create an instance of it is the LevelManager class, so you will always know that the Level instance is created in the LevelManager.

While true, that's a private constructor, not protected.
David
ahaha sorry, i read wrong. Shall i delete my post?
A: 

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.

Nick Lewis
A: 

For factory methods with side-effects.

class mine {

  private:
    mine () {};

  protected:
    mine(int id) : m_id(id) {};

   int m_id;
   static int m_count;

  public:
    mine* CreateOneOfMe() {
         return mine(m_count++);
    }

    int GetId() { return m_id; }

 };

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.

sean riley