473,379 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Pure virtual destructor in template class

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?
Nov 18 '08 #1
7 7300
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?
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?
If your destructor doesn't do anything, you can give it an empty body
right in the class definition. Since you have other virtual functions
in your interface, they will be pure and the destructor doesn't have to
be. But if you just *want* your destructor pure, your solution is just
what the doctor ordered. Idiomatic.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 18 '08 #2
On 2008-11-18 11:33:49 -0500, Tonni Tielens <to**********@gmail.comsaid:
I'm trying to create a pure virtual class describing an interface.
There's no such thing as a pure virtual class. The term you're looking
for is abstract class.
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.
It is.
Is there a correct way to do this or
should I leave the pure virtual destructor out?
You can't leave the destructor out. Try it.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 18 '08 #3
On 2008-11-18 11:52:08 -0500, Victor Bazarov <v.********@comAcast.netsaid:
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. For example, if a class that implements
java.util.List (which is, roughly, a linked list) also implements
java.util.RandomAccess, it announces that it supports constant-time
random access to elements, so a loop like this:

for (int i = 0, n = list.size(); i <n; i++)
list.get(i);

will supposedly be faster than a loop like this:

for (Iterator i = list.iterator(); i.hasNext(); )
i.next();

You'd use a runtime type check to decide which way to go.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 18 '08 #4
On Nov 18, 5:52*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
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.
Nov 18 '08 #5
Tonni Tielens wrote:
On Nov 18, 5:52 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>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.
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.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 18 '08 #6
On Nov 18, 7:42*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
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. :)
Nov 18 '08 #7
On Nov 18, 6:06*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-11-18 11:52:08 -0500, Victor Bazarov <v.Abaza...@comAcast.netsaid:
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:ja*********@gmail.com
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
Nov 18 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Todd Aspeotis | last post by:
Hello all. I've been having some very weird compiler errors in regards to my classes. I have my base node class here: template <class T> class CNode : public CManaged //-- Base node used to...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
3
by: Dmitry Prokoptsev | last post by:
Hello, I need to write a class for exceptions which can be thrown, caught, stored and thrown once again. I have written the following code: --- begin --- #include <string> class Exception...
4
by: Eric | last post by:
I was wondering what people thought about the information found at: http://g.oswego.edu/dl/mood/C++AsIDL.html Specifically, I am interested in the following recommendation: ---- Since...
3
by: YellowMaple | last post by:
Is it possible to have pure virtual functions defined in a template class? I'm trying to do something like this: template <typename T> class Singleton { public: static T& getInstance(void) {...
2
by: manjuks | last post by:
Hi, I have defined pure virtual destructor in base class. As far as I know pure virtual function makes us to redefine the same function in derived classes also. and also we can not create a...
7
by: arnaudk | last post by:
How come it's possible to have a base class with a pure virtual destructor and nonetheless one has to implement the destructor? Doesn't pure virtual mean that there is no implementation of that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.