Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pure virtual destructor in template class

Reply
Thread Tools

Pure virtual destructor in template class

 
 
Tonni Tielens
Guest
Posts: n/a
 
      11-18-2008
I'm trying to create a pure virtual class describing an interface.
Normally, when I do this I make the destructor pure virtual so that,
even if there are no members in the class, it cannot be instantiated.

The difference now is that I'm making a generic interface with
template arguments. Template classes should be defined in the header
file, but it is not allowed for a destructor's definition to be in the
class definition if the destructor is pure virtual. Atleast not with
GCC -pedantic and I understand this is correct behavior. I'm unsure on
how to solve this. I know I don't really have to put a pure virtual
destructor in the class, but I think it's good practice so if it's
possible I would like to stick to this.

My code looks like the following:


template <typename TypeA, typename TypeB>
struct MyInterface
{
virtual ~MyInterface() = 0;

virtual void Foo(TypeA a, TypeB b) = 0;
};


I already found that I can resolve the compilation errors, by adding


template <typename TypeA, typename TypeB>
MyInterface<TypeA, TypeB>::~MyInterface() {}


in the same file after the class definition, but I'm not sure if this
is the common way to do this. Is there a correct way to do this or
should I leave the pure virtual destructor out?
 
Reply With Quote
 
 
 
 
Tonni Tielens
Guest
Posts: n/a
 
      11-18-2008
On Nov 18, 5:52*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> Why would you have an interface with no other members? *Wouldn't it be
> pretty much useless as an interface?


It's just a way (and I believe a common way) of telling a class is
abstract without having the need to have any abstract members.
Consider Java or C# where you can define an interface without having
any methods. Completely useless, but perfectly legal. Normally, since
it is almost no effort, I make the destructor pure virtual, but since
it is more effort here and I'm going to add pure virtual methods
anyway, I will leave it out and let the compiler generate a default
one.

Thanks for the replies.
 
Reply With Quote
 
 
 
 
Tonni Tielens
Guest
Posts: n/a
 
      11-18-2008
On Nov 18, 7:42*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> No, don't let the compiler do it because in that case it wouldn't be
> virtual. *You *do* need the destructor to be virtual, trust me.



Ah, you're completely right. I wasn't thinking while posting that.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-18-2008
On Nov 18, 6:06*pm, Pete Becker <(E-Mail Removed)> wrote:
> On 2008-11-18 11:52:08 -0500, Victor Bazarov <(E-Mail Removed)> said:


> > Tonni Tielens wrote:
> >> I'm trying to create a pure virtual class describing an
> >> interface. Normally, when I do this I make the destructor
> >> pure virtual so that, even if there are no members in the
> >> class, it cannot be instantiated.


> > Why would you have an interface with no other members?
> > *Wouldn't it be pretty much useless as an interface?


> It's a Java thing.


I don't think it's only Java. It's known as a tagging
interface, and it potentially has a role in any staticly typed
language which supports polymorphism. I think I've actually
used it once in C++; C++ usually has other ways of solving the
problem, however, which are generally preferred (because they
can be made to work with non class types as well). Even in the
standard, the iterator_tag hierarchy could be considered an
example of this.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
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
pure virtual destructor hpsoar C++ 0 02-04-2009 02:33 PM
Pure virtual destructor David Côme C++ 6 12-27-2007 12:54 AM
Non-template class from a template base class with pure virtual methods vilarneto@gmail.com C++ 2 03-25-2007 08:19 PM
Necessity of pure virtual destructor? WittyGuy C++ 37 07-02-2005 01:47 PM
Pure virtual destructor santosh C++ 11 06-27-2005 05:48 PM



Advertisments