Constructor protected

J

John Harrison

away said:
Why some classes are seen to has their constructors declared as
"protected"?
Thanks!

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
 
R

Rubén Campos

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

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.

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.
 
R

Rolf Magnus

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,

Which would be the definition of "abstract class".
and for abstract classes with all their methods implemented, too.

Implementing a function an making it pure virtual are not mutually
exclusive.
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.

If it doens't have members, there is nothing to initialize. Also, if you
don't write an own constructor, the compiler will generate a default
constructor for you. There is nothing wrong about using that if it does
what you need.
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.

I guess you mean "...should not know anything...". Anyway, there is often
some knowledge needed, and in many cases, you can use knowledge about the
internals of a class to get a significant speed optimization.
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.

Usually yes, but that doesn't have anything to do with the constructor's
access specification.
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.
 
J

John Harrison

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.

That's my main point, in this context having a protected constructor doesn't
achieve anything, except maybe confusing a few people like the OP.

john
 
R

Ron Natalie

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.

An abstract class doesn't much care whether the constructor is public or not.
You can't construct one anyhow.
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.

Classes always have constructors (whether you define them or not).
Most abstract uses don't have any data anyhow.
 
A

Andy

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.

Protected constructors often mean things other than
"dont-instantiate-me-but-my-child". That's why it is not replaceable
by an abstract class.

You might want to make a singleton object and also make the class
available for extension. In that case an abstract class cannot do what
a set of protected constructors can.


Cheers,
Andy
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top