473,327 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Purpose of protected constructors

I saw an implementaiton with protected constrcutors.
What could be use of this construct be?
Jun 27 '08 #1
8 9866
pa********@hotmail.com wrote:
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.
Jun 27 '08 #2
pa********@hotmail.com wrote:
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_streambuf' class [template] from the standard library.

Of course, the "abstractness" 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.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #3

<pa********@hotmail.comwrote in message
news:1a**********************************@j1g2000p rb.googlegroups.com...
>I saw an implementaiton with protected constrcutors.
What could be use of this construct be?
Another purpose of a protected *or private* constructor would be to allow a
static member function to instance it's own class.

It is a very handy pattern. One that allows a single class to serve a both a
model AND it's own instance-controller.

Java uses the paradigm a lot.

R.A. Nagy
http://www.Soft9000.com

Jun 27 '08 #4
"Andrey Tarasevich"
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?

Fraser.
Jun 27 '08 #5
Noah Roberts wrote:
There are numerous reasons why you would not want to allow direct
instantiation of a class or base.
Can you give an example of such situation, where it cannot be done by
putting a pure virtual function in the base class?
Jun 27 '08 #6
Juha Nieminen wrote:
Noah Roberts wrote:
>There are numerous reasons why you would not want to allow direct
instantiation of a class or base.

Can you give an example of such situation, where it cannot be done by
putting a pure virtual function in the base class?
struct Base
{
private:
struct impl;
impl * pimpl;
protected:
Base();
public:
boost::shared_ptr<Baseconstruct_base();

/* ... functionality - virtuals but no pure */
};

struct Derived
{
private:
struct impl; impl * pimpl;
Derived();
public:
boost::shared_ptr<Derivedconstruct_derived();

/* behavior overrides */
};

Derived.cpp:

Derived::Derived() : Base(), pimpl(new impl) {}

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.

Those are the cases I can think of on the spot.
Jun 27 '08 #7
Test Name wrote:
"Andrey Tarasevich"
>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_streambuf' base class is implemented in the
C++ standard library.

What specific loopholes are you referring to?

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #8
Fraser Ross wrote:
...
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_streambuf' as an ABC, while in fact it does not satisfy
the standard C++ definition if ABC at all :)

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Beach Potato | last post by:
I guess I've been out of C++ for a while, since right now I don't seem to get a simple solution for overriding inherited constrictors. What worked in Borland C++ & Pascal (and Java, if I remember...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
4
by: Grey Plastic | last post by:
I have several classes that all keep track of static data. However, the manner that they keep track of static data is identical, and so I'm using the template<class Child> class Parent { ... };...
6
by: away | last post by:
Why some classes are seen to has their constructors declared as "protected"? Thanks!
3
by: MegaGreg | last post by:
I'm trying to inherit from System.Diagnostics.ProcessThread but I keep getting the same error no matter what I try:The namespace 'CSRSystem' already contains a definition for 'Program' None of...
2
by: talkingpidgin | last post by:
I am trying to figure out why it is not conventional to use protected constructors in abstract classes since the only time they should be called is by the constructors of it's derived classes. Is...
0
by: Nyman | last post by:
Alan arvostuksesta kertoo varsin suoraan se miten alan ammattityöväestö suhtautuu tekemiseensä. Siitä voidaan ottaa esimerkkejä laajasti. Ammattilaiset haluavat usein myös jälkikasvustaan työnsä...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.