By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,042 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,042 IT Pros & Developers. It's quick & easy.

Constructor protected

P: n/a
Why some classes are seen to has their constructors declared as "protected"?
Thanks!
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"away" <Gu*******@spambs.com> wrote in message
news:AF*******************@bgtnsc04-news.ops.worldnet.att.net...
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
Jul 22 '05 #2

P: n/a

"John Harrison" <jo*************@hotmail.com> escribió en el mensaje
news:2r*************@uni-berlin.de...

"away" <Gu*******@spambs.com> wrote in message
news:AF*******************@bgtnsc04-news.ops.worldnet.att.net...
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


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.
Jul 22 '05 #3

P: n/a
Rubén Campos wrote:
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.

Jul 22 '05 #4

P: n/a
>
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
Jul 22 '05 #5

P: n/a

"Rubén Campos" <Ru**********@robotica.uv.es> wrote in message news:ci**********@peque.uv.es...

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.


Jul 22 '05 #6

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2r*************@uni-berlin.de>...
"away" <Gu*******@spambs.com> wrote in message
news:AF*******************@bgtnsc04-news.ops.worldnet.att.net...
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.


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
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.