Abstract base class with no virtual methods?

R

Roy Smith

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?
 
V

Victor Bazarov

Roy said:
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
 
A

Andrey Tarasevich

Roy said:
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.
 
R

Roy Smith

Andrey Tarasevich said:
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.

That sounds pretty straight-forward. Thanks.
 
D

Duane Hebert

Roy Smith said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top