A
away
Why some classes are seen to has their constructors declared as "protected"?
Thanks!
Thanks!
away said:Why some classes are seen to has their constructors declared as
"protected"?
Thanks!
John Harrison said:Because the constructor is meant to be used by a derived class only.
If all the constructors are protected then I guess its a way of saying don't
declare a variable with this class, derive your own class from this class
and use that. But I'd also say there a better ways of doing that, just
making the class abstract will also have the same effect.
john
Rubén Campos said:An abstract class should not include any public constructor, just because
it is not allowed to create instances directly from it.
Why?
This is true for abstract classes including pure virtual member functions,
and for abstract classes with all their methods implemented, too.
But an abstract class should still include at least one protected
constructor (and, optionally, additional protected and or private
constructors). The reason is that a class, abstract or not, having data
members or not, always have the responsibility of initializing itself.
In a correct object-oriented scheme, all class' data members should be
declared as private. Thus, a class always should have access to its own
(private) data members, and any more. In fact, deerived classes should not
know nothing about their base classes' data members.
Consequently, derived classes should not have access to their base
classes' data members, but should access them through their base classes'
public and/or protected interface.
So protected constructors are required by derived classes to
initialize their base classes in a coherent way.
However, for the derived class, there is no difference whether the base
class's constructor is public or protected. In the case of an abstract base
class, there is even no difference at all, since that class can't be
instantiated directly anyway.
Rubén Campos said:An abstract class should not include any public constructor, just because it
is not allowed to create instances directly from it. This is true for
abstract classes including pure virtual member functions, and for abstract
classes with all their methods implemented, too.
But an abstract class should still include at least one protected
constructor (and, optionally, additional protected and or private
constructors). The reason is that a class, abstract or not, having data
members or not, always have the responsibility of initializing itself.
John Harrison said:Because the constructor is meant to be used by a derived class only.
If all the constructors are protected then I guess its a way of saying don't
declare a variable with this class, derive your own class from this class
and use that. But I'd also say there a better ways of doing that, just
making the class abstract will also have the same effect.
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.