473,513 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructor protected

Why some classes are seen to has their constructors declared as "protected"?
Thanks!
Jul 22 '05 #1
6 2199

"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

"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
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
>
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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2160
by: Amadrias | last post by:
Hi all, I am using a class to transport some data over the network. I then added the attribute to the class. My problem is that this class is part of a framework and that I do not want...
6
4974
by: cppaddict | last post by:
Hi, I know that C++ does not have an explicit super() constructor for calling a Base class constructor from a Derived class's constructor, but my understanding is that C++ implements this...
4
6101
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
5
5294
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
10
3845
by: William Stacey | last post by:
I know the following is not allowed, but shouldn't it be? sharedObject is part of Derived and should be able to be set in the constructor - no? tia public abstract class Base1 { protected...
11
2389
by: RickHodder | last post by:
I'm having a problem, and here is a simplified example of code that demonstrates it: public class BizObj { public string TableName=""; private DataSet oData; public BizObj()
1
3369
by: herc | last post by:
Here is what MSDN says about the constructor that is needed when implementing the ISerializable interface: "The ISerializable interface implies a constructor with the signature constructor...
10
3433
by: siddhu | last post by:
Dear Experts, I want to make a class whose objects can be created only on heap. I used the following approach. class ss { ss(){} public: void* operator new(size_t sz)
4
2929
by: softwaredoug | last post by:
Here is some test code I've been attempting to compile (Visual Studio 2003) test.h: class Base { protected: Base() {} public:
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.