Purpose of protected constructors

P

parag_paul

I saw an implementaiton with protected constrcutors.
What could be use of this construct be?
 
N

Noah Roberts

I saw an implementaiton with protected constrcutors.
What could be use of this construct be?

The purpose would be to keep clients from instantiating the base but
still allowing it to be subclassed. I would gather that the default
constructor is not sufficient to build the base and that a "custom"
constructor needs to be called by the subclass during its own construction.

There are numerous reasons why you would not want to allow direct
instantiation of a class or base.
 
A

Andrey Tarasevich

I saw an implementaiton with protected constrcutors.
What could be use of this construct be?

Protected constructor is one possible way to implement the OOP concept
of "abstract base class" in C++. Protected constructor indicates that
the class is not supposed to be instantiated as a standalone object, but
only as a base class subobject of another class. A classic example would
be 'std::basic_streambuf' class [template] from the standard library.

Of course, the "abstractness" of a class can be reflected in C++ code by
other means, like introduction of at least one pure virtual method. A
protected constructor is just an alternative approach.
 
R

R.A. Nagy

I saw an implementaiton with protected constrcutors.
What could be use of this construct be?

Another purpose of a protected *or private* constructor would be to allow a
static member function to instance it's own class.

It is a very handy pattern. One that allows a single class to serve a both a
model AND it's own instance-controller.

Java uses the paradigm a lot.

R.A. Nagy
http://www.Soft9000.com
 
T

Test Name

"Andrey Tarasevich"
Protected constructor is one possible way to implement the OOP concept
of "abstract base class" in C++. Protected constructor indicates that
the class is not supposed to be instantiated as a standalone object, but
only as a base class subobject of another class.

There are some loopholes to that method. Are there any experts that
have recommended this?

Fraser.
 
J

Juha Nieminen

Noah said:
There are numerous reasons why you would not want to allow direct
instantiation of a class or base.

Can you give an example of such situation, where it cannot be done by
putting a pure virtual function in the base class?
 
N

Noah Roberts

Juha said:
Can you give an example of such situation, where it cannot be done by
putting a pure virtual function in the base class?

struct Base
{
private:
struct impl;
impl * pimpl;
protected:
Base();
public:
boost::shared_ptr<Base> construct_base();

/* ... functionality - virtuals but no pure */
};

struct Derived
{
private:
struct impl; impl * pimpl;
Derived();
public:
boost::shared_ptr<Derived> construct_derived();

/* behavior overrides */
};

Derived.cpp:

Derived::Derived() : Base(), pimpl(new impl) {}

You might consider this idiom any time you want to be sure an object is
never constructed on the stack and never NOT put in a shared_ptr (such
as when also subclassing shared_ptr_from_this).

Another example would be the use of a factory. You want to be sure the
object is always created through the factory but it also needs to be
constructed. The abstraction a factory generates is usually a real
abstract object, but maybe that's not appropriate in all cases. You can
toss in a pure virtual for no reason other than to not have to protect
your constructor, but what's the point in that?? Furthermore you'd
still want to protect construction of derived objects and might wish to
allow them also to be extended by subclassing.

Finally, protecting the constructor says something about the object. It
says that constructing this object through the constructor is not in its
public interface.

Those are the cases I can think of on the spot.
 
A

Andrey Tarasevich

Test said:
"Andrey Tarasevich"

There are some loopholes to that method. Are there any experts that
have recommended this?
...

Well, apparently there are, since, as I said before, this is how
abstractness of 'std::basic_streambuf' base class is implemented in the
C++ standard library.

What specific loopholes are you referring to?
 
J

James Kanze

Can you give an example of such situation, where it cannot be
done by putting a pure virtual function in the base class?

That's not the point. The point is that if no one but a derived
class should call the constructor, logically, it should be
protected. I make the constructors of my abstract base classes
protected even when they have pure virtual functions. (Of
course, most abstract base classes only have pure virtual
functions, and no data---in such cases, I generally won't go to
the bother of declaring a constructor at all.)
 
F

Fraser Ross

"Andrey Tarasevich"
What specific loopholes are you referring to?

As someones said a static member function could invoke a constructor.
Also a friend could. Possibly a nested class also.

The standard does not describe an ABC as you have.

Fraser.
 
N

Noah Roberts

Noah said:
struct Base
{
private:
struct impl;
impl * pimpl;
protected:
Base();
public:
boost::shared_ptr<Base> construct_base();

These construct_xxx functions were supposed to be static.
 
A

Andrey Tarasevich

Fraser said:
...
As someones said a static member function could invoke a constructor.
Also a friend could. Possibly a nested class also.

So what? Just like with any C++ feature, whether it offers any
protection from malicious intent is secondary or irrelevant. Protected
constructor only _expresses_ the purpose of the class. Once you are
aware of that purpose, it is up to you to decide to use it properly or
improperly.
The standard does not describe an ABC as you have.

I explicitly stated in my original message that I'm talking about the
concept of ABC in the general OOP sense of the term, not in strict C++
sense. C++ ABC is a way to implement OOP ABC, which does not mean that
this is the only way to implement the OOP ABC. Moreover, OOP ABC is just
a design concept that does not require to be implemented/enforced at the
language level at all. From the OOP point of view, a class in an ABC if
I said it is ABC. The opportunity to _enforce_ that fact to some degree
by using some C++ feature (like a protected constructor or a pure
virtual method) is just an optional icing on the cake.

Also, you apparently failed to notice that the standard itself refers to
'std::basic_streambuf' as an ABC, while in fact it does not satisfy
the standard C++ definition if ABC at all :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,473
Latest member
ChristelPe

Latest Threads

Top