473,394 Members | 1,865 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,394 software developers and data experts.

Protected Abstract Class Contructors

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 it simply
because it's not strictly necessary since an abstract class can't be
instantiated anyways? It just seems like it would be clearer to make
the ctor protected so I figured there must be some reason for it.

Also I am trying to put together a list of when protected ctors in
abstract classes would actually be practical/necessary. I know there
has been a couple previous threads on this but they are not very clear.
I will start by listing what I have interpreted from them.

-Singleton interfaces. There is a lot of talk of singleton classes. I
think this is because an abstract protected ctor would allow you to
create any of an abstract base class' derived classes, but only one of
those derived classes. i.e. If all derived classes are a "type" of the
base class, then doing this would only let you create one object of a
certain type. Please tell me if I am on track with this since I have
read about singleton classes but I don't have experience implementing
them.

I will list more tomorrow when I have time to go over the previous
posts again.

Nov 28 '06 #1
2 2248

<ta***********@gmail.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
>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 it simply
because it's not strictly necessary since an abstract class can't be
instantiated anyways? It just seems like it would be clearer to make
the ctor protected so I figured there must be some reason for it.

Also I am trying to put together a list of when protected ctors in
abstract classes would actually be practical/necessary. I know there
has been a couple previous threads on this but they are not very clear.
I will start by listing what I have interpreted from them.

-Singleton interfaces. There is a lot of talk of singleton classes. I
think this is because an abstract protected ctor would allow you to
create any of an abstract base class' derived classes, but only one of
those derived classes. i.e. If all derived classes are a "type" of the
base class, then doing this would only let you create one object of a
certain type. Please tell me if I am on track with this since I have
read about singleton classes but I don't have experience implementing
them.

I will list more tomorrow when I have time to go over the previous
posts again.
Abstract base classes probably shouldn't have constructors at all. Whatever
you do in the constructor is effectively implementation and an abstract base
class should be pure interface. A class derived from an abstract base class
which doesn't fully implement the interface doesn't really need a protected
constructor since no objects of that type can be declared anyway.

// abstract base class
class IThing
{
public:
virtual int afunc() const = 0;
virtual double bfunc() const = 0;
virtual ~IThing() {}
};

// partial implementation
class BThing : public IThing
{
private:
int a;
public:
BThing(int ia) : a(ia) {} // could be protected, but why?
virtual int afunc() const {return a;}
};

Cy
Nov 28 '06 #2
On Tue, 28 Nov 2006 01:21:42 +0000, Cy Edmunds wrote:
>
Abstract base classes probably shouldn't have constructors at all. Whatever
you do in the constructor is effectively implementation and an abstract base
class should be pure interface. A class derived from an abstract base class
which doesn't fully implement the interface doesn't really need a protected
constructor since no objects of that type can be declared anyway.
Abstract-as-interface is a design pattern, and I don't think that
abstracts can never be reasonably used outside that pattern.

On occasion, I've not only provided implementions for methods in abstract
classes, I've provided implementations for pure virtual methods in
abstract classes. Not often, but a couple of times it seemed the best
place to put the code.

The derived classes _had_ to provide their own implementations of the
method, the design required it. But nearly all of what needed to be done
in those derived implementations was identical, and could be put in the
base class.
#include <iostream>
using namespace std;

class Foo {
public:
virtual void foo() = 0;
};

void Foo::foo() {
cout << "Foo::foo()" << endl;
}

class Bar : public Foo {
public:
virtual void foo() {
Foo::foo();
cout << "Bar::foo()" << endl;
}
};

int main(int, char**) {
Bar b;
b.foo();
return 0;
}

--
The other day I was talking with a Democrat friend about the election.
She'd remarked, with equal amounts of sarcasm and good-natured ribbing,
that the GOP had two years to build utopia. I thought about that later
while walking Jasper around the block, and thought, no; they're not about
building utopia. Personally, I'm interested in keeping other people from
building Utopia, because the more you believe you can create heaven on
earth the more likely you are to set up guillotines in the public square
to hasten the process.
- James Lileks

Nov 28 '06 #3

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

Similar topics

6
by: away | last post by:
Why some classes are seen to has their constructors declared as "protected"? Thanks!
4
by: Jason Shohet | last post by:
A label on an ascx control has a corresponding declaration in the c# code-behind page. I was curious what would happen if I made that declaration 'PRIVATE' instead of 'PROTECTED'. The only things...
7
by: TT (Tom Tempelaere) | last post by:
Hi there The line marked *** doesn't compile. I wonder why the designers of C# decided to disallow this. Rationale? Are there plans to change this? This feature forces me to make things public...
4
by: Brian Lowe | last post by:
I have several aspx pages that include a couple of asp:placeholder controls, an example is myPage.aspx. The page inherits from a custom class 'myClass' which inherits System.Web.UI.Page which...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: Steven Livingstone | last post by:
I know this is not supported, but i'm just looking for some reasoning and perhaps debate. Is there are reason why this is not supported as in the words of manhy a French poet "it's doing my head...
3
by: ssg31415926 | last post by:
I have an abstract base class from which has concrete subclasses. I have a method on the base class marked as 'protected internal'. I have another class in the same assembly which holds a...
8
by: Jordan | last post by:
AFAIK there are two ways to expose members of a base class to a derived or child class: 1. declare the members public in the base class 2. declare them as 'protected' in the base class Is...
2
by: jehugaleahsa | last post by:
Hello: I have a public abstract class. The concrete subclasses must pass an instance of an internal class to the abstract class' ctor. I like to make the constructors of my abstract classes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.