Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Abstract base class with no virtual methods?

Reply
Thread Tools

Abstract base class with no virtual methods?

 
 
Roy Smith
Guest
Posts: n/a
 
      11-12-2005
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?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-12-2005
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


 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-12-2005
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


 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      11-12-2005
In article <(E-Mail Removed)>,
Andrey Tarasevich <(E-Mail Removed)> 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.
 
Reply With Quote
 
JefferyRen@gmail.com
Guest
Posts: n/a
 
      11-12-2005

Roy Smith wrote:
> In article <(E-Mail Removed)>,
> Andrey Tarasevich <(E-Mail Removed)> 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.


 
Reply With Quote
 
Duane Hebert
Guest
Posts: n/a
 
      11-18-2005

"Roy Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the difference between abstract class and pure abstract class? skishorev@yahoo.co.in C++ 4 05-17-2006 08:07 AM
Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table? sojin C++ 12 04-07-2006 09:02 AM
"Virtual constructor" abstract base class - feasibility? Asfand Yar Qazi C++ 8 01-25-2006 01:49 AM
Deriving abstract class from non-abstract class Matthias Kaeppler Java 1 05-22-2005 01:28 PM
Abstract base class + virtual Method Luis G Hernandez C++ 5 03-06-2004 01:40 PM



Advertisments