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

Abstract base class with no virtual methods?

I want to have three classes; Parent, Child1, and Child2. Parent is to be
an abstract base class with Child1 and Child2 concrete classes derived from
Parent.

Normally, to do that, Parent would have some virtual method defined as
"=0", and implemented in the two children. The problem I have is that the
only difference between the three classes is in the constructor. In fact,
the two child classes won't define any methods at all other than their
constructors.

What's the best way to make it impossible to instantiate an instance of
Parent in a situation like this?
Nov 22 '05 #1
5 1983
Roy Smith wrote:
I want to have three classes; Parent, Child1, and Child2. Parent is
to be an abstract base class with Child1 and Child2 concrete classes
derived from Parent.

Normally, to do that, Parent would have some virtual method defined as
"=0", and implemented in the two children. The problem I have is
that the only difference between the three classes is in the
constructor. In fact, the two child classes won't define any methods
at all other than their constructors.

What's the best way to make it impossible to instantiate an instance
of Parent in a situation like this?


The simplest of course is to declare the destructor virtual (and you do
need that if you intend to destroy 'Child1' or 'Child2' object through
a pointer to 'Parent') and declare it pure, and then define it outside
of the class (empty-bodied if it has nothing to do in 'Parent').

V
Nov 22 '05 #2
Roy Smith wrote:
I want to have three classes; Parent, Child1, and Child2. Parent is to be
an abstract base class with Child1 and Child2 concrete classes derived from
Parent.

Normally, to do that, Parent would have some virtual method defined as
"=0", and implemented in the two children. The problem I have is that the
only difference between the three classes is in the constructor. In fact,
the two child classes won't define any methods at all other than their
constructors.

What's the best way to make it impossible to instantiate an instance of
Parent in a situation like this?


If you don't need _any_ polymorphic behavior from your 'Parent' class (including
polymorphic deletion), I'd suggest that you simply declare all 'Parent's
constructors with 'protected' access specification.

--
Best regards,
Andrey Tarasevich
Nov 22 '05 #3
In article <11*************@news.supernews.com>,
Andrey Tarasevich <an**************@hotmail.com> wrote:
Roy Smith wrote:
I want to have three classes; Parent, Child1, and Child2. Parent is to be
an abstract base class with Child1 and Child2 concrete classes derived from
Parent.

Normally, to do that, Parent would have some virtual method defined as
"=0", and implemented in the two children. The problem I have is that the
only difference between the three classes is in the constructor. In fact,
the two child classes won't define any methods at all other than their
constructors.

What's the best way to make it impossible to instantiate an instance of
Parent in a situation like this?


If you don't need _any_ polymorphic behavior from your 'Parent' class
(including
polymorphic deletion), I'd suggest that you simply declare all 'Parent's
constructors with 'protected' access specification.

--
Best regards,
Andrey Tarasevich


That sounds pretty straight-forward. Thanks.
Nov 22 '05 #4

Roy Smith wrote:
In article <11*************@news.supernews.com>,
Andrey Tarasevich <an**************@hotmail.com> wrote:
Roy Smith wrote:
I want to have three classes; Parent, Child1, and Child2. Parent is to be
an abstract base class with Child1 and Child2 concrete classes derived from
Parent.

Normally, to do that, Parent would have some virtual method defined as
"=0", and implemented in the two children. The problem I have is that the
only difference between the three classes is in the constructor. In fact,
the two child classes won't define any methods at all other than their
constructors.

What's the best way to make it impossible to instantiate an instance of
Parent in a situation like this?


If you don't need _any_ polymorphic behavior from your 'Parent' class
(including
polymorphic deletion), I'd suggest that you simply declare all 'Parent's
constructors with 'protected' access specification.

--
Best regards,
Andrey Tarasevich


That sounds pretty straight-forward. Thanks.


Nov 22 '05 #5

"Roy Smith" <ro*@panix.com> wrote in message
news:ro***********************@reader2.panix.com.. .
I want to have three classes; Parent, Child1, and Child2. Parent is to be
an abstract base class with Child1 and Child2 concrete classes derived
from
Parent.

Normally, to do that, Parent would have some virtual method defined as
"=0", and implemented in the two children. The problem I have is that the
only difference between the three classes is in the constructor. In fact,
the two child classes won't define any methods at all other than their
constructors.

What's the best way to make it impossible to instantiate an instance of
Parent in a situation like this?


Make the dtor virtual. It needs to be virtual anyway if you want
to use it polymorphically. Why do you need the base to be
abstract? Are you just trying to prevent it being instantiated?
If that's the case, you may make the dtor pure virtual but then
I think you'll need to implement it in the derived classes instead of
using a default one.
Nov 22 '05 #6

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

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
10
by: Bjorn | last post by:
I'm using interfaces in C++ by declaring classes with only pure virtual methods. If then someone wants to implement the interface they needs to inherit from the class. If the implementing class...
8
by: Dev | last post by:
Hello, Why an Abstract Base Class cannot be instantiated ? Does anybody know of the object construction internals ? What is the missing information that prevents the construction ? TIA....
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
6
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
18
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
3
by: WithPit | last post by:
I am trying to create an managed wrapper but have some problems with it by using abstract classes. In my unmanaged library code i had the following three classes with the following hierarchy ...
9
by: silversurfer2025 | last post by:
Hello everyone, I am currently having problems with a C++ abstract class. I have a class FrameWork.h which defines some methods (of which some are abstract, i.e. virtual void method() = 0). In...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.