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

Abstract base class with no virtual methods?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.