473,903 Members | 5,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Typ eA, TypeB>::~MyInte rface() {}
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 7394
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<Typ eA, TypeB>::~MyInte rface() {}
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**********@g mail.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<Typ eA, TypeB>::~MyInte rface() {}
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.********@com Acast.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.Rando mAccess, 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...@com Acast.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...@com Acast.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...@com Acast.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...@versatile coding.comwrote :
On 2008-11-18 11:52:08 -0500, Victor Bazarov <v.Abaza...@com Acast.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 objektorientier ter Datenverarbeitu ng
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
1386
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 traverse either dynamic or static data structures //-- transparently. { public: //-- Declarations:
11
4383
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 it inline. Like this.
37
4194
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
3499
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
4596
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 { public:
4
1881
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 interface classes cannot be directly instantiated, yet serve as virtual base classes for implementations, the constructors should take no arguments and should be listed as protected. Also, for similar
3
3334
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) { if(m_pInstance == NULL)
2
4089
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 objects of class which contains pure virtual functions but we can create pointer or reference of the same. My problem is I have defined a pure virtual function in base class, so I have to redefine the same in derived class. When I did the same...
7
3708
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 method in the base class and the implementation is left entirely up to derived classes? class Base { public: Base() {} virtual ~Base() = 0; // pure virtual destructor } Base::~Base() // <-- Linktime error if {} // this...
0
9999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10500
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9684
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4308
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3324
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.