Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Abstract base class with no virtual methods? (http://www.velocityreviews.com/forums/t449741-abstract-base-class-with-no-virtual-methods.html)

Roy Smith 11-12-2005 01:30 AM

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?

Victor Bazarov 11-12-2005 01:33 AM

Re: Abstract base class with no virtual methods?
 
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



Andrey Tarasevich 11-12-2005 02:05 AM

Re: Abstract base class with no virtual methods?
 
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



Roy Smith 11-12-2005 12:33 PM

Re: Abstract base class with no virtual methods?
 
In article <11najestinutmc5@news.supernews.com>,
Andrey Tarasevich <andreytarasevich@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.

JefferyRen@gmail.com 11-12-2005 01:42 PM

Re: Abstract base class with no virtual methods?
 

Roy Smith wrote:
> In article <11najestinutmc5@news.supernews.com>,
> Andrey Tarasevich <andreytarasevich@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.



Duane Hebert 11-18-2005 12:27 PM

Re: Abstract base class with no virtual methods?
 

"Roy Smith" <roy@panix.com> wrote in message
news:roy-4AD839.20302111112005@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.




All times are GMT. The time now is 08:38 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.