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.
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_str eambuf' class [template] from the standard library.
Of course, the "abstractne ss" 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.
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?
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.
>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?
...
Well, apparently there are, since, as I said before, this is how
abstractness of 'std::basic_str eambuf' base class is implemented in the
C++ standard library.
...
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_str eambuf' as an ABC, while in fact it does not satisfy
the standard C++ definition if ABC at all :)
Comment