473,386 Members | 1,647 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,386 software developers and data experts.

Interfaces and non-virtual destructors

If i have an interface like

template <class K>
struct IComparableTo
{ virtual int compareTo(const K& key) const = 0;
};

some compilers (e.g. gcc) warn me about that the class has virtual
functions but a non-virtual destructor. While this can be helpful in
some cases, it can be annoying too.
If the interface is a template like in the example I get hundreds of
warnings for each type instantiation in each compilation unit.

From the applications point of view it might be not reasonable to
delete the entire object through a particular interface. In such cases I
usually write

template <class K>
struct IComparableTo
{ virtual int compareTo(const K& key) const = 0;
protected:
~ICompareableTo() {}
};

to avoid accidental deletion. But the warnings still hide other, more
important messages.

Is there another way to declare interfaces?
Or are there other good reasons to have virtual destructors on interface
classes?
Marcel
Aug 23 '08 #1
6 2169
On 2008-08-23 14:02, Marcel Müller wrote:
If i have an interface like

template <class K>
struct IComparableTo
{ virtual int compareTo(const K& key) const = 0;
};

some compilers (e.g. gcc) warn me about that the class has virtual
functions but a non-virtual destructor. While this can be helpful in
some cases, it can be annoying too.
If the interface is a template like in the example I get hundreds of
warnings for each type instantiation in each compilation unit.

From the applications point of view it might be not reasonable to
delete the entire object through a particular interface. In such cases I
usually write

template <class K>
struct IComparableTo
{ virtual int compareTo(const K& key) const = 0;
protected:
~ICompareableTo() {}
};

to avoid accidental deletion. But the warnings still hide other, more
important messages.

Is there another way to declare interfaces?
Or are there other good reasons to have virtual destructors on interface
classes?
Yes, the reason is the same as why you should have a virtual destructor
in a base-class. If someone have a pointer of type IComarableTo which
points to a class implementing the interface and then use delete on the
pointer you want IComparableTo to have a virtual destructor.

See also:
http://www.parashift.com/c++-faq-lit....html#faq-20.7

--
Erik Wikström
Aug 23 '08 #2
On 2008-08-23 08:02:54 -0400, Marcel Müller
<ne**********@spamgourmet.orgsaid:
If i have an interface like

template <class K>
struct IComparableTo
{ virtual int compareTo(const K& key) const = 0;
};

some compilers (e.g. gcc) warn me about that the class has virtual
functions but a non-virtual destructor. While this can be helpful in
some cases, it can be annoying too.
If the interface is a template like in the example I get hundreds of
warnings for each type instantiation in each compilation unit.
That's what happens when compiler writers assume that they know more
about your application than you do. Surely, though, there's a
command-line switch to turn off this warning.

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

Aug 23 '08 #3
Marcel Müller wrote:
If i have an interface like

template <class K>
struct IComparableTo
{ virtual int compareTo(const K& key) const = 0;
};

some compilers (e.g. gcc) warn me about that the class has virtual
functions but a non-virtual destructor. While this can be helpful in
some cases, it can be annoying too.
If the interface is a template like in the example I get hundreds of
warnings for each type instantiation in each compilation unit.

From the applications point of view it might be not reasonable to
delete the entire object through a particular interface. In such cases I
usually write

template <class K>
struct IComparableTo
{ virtual int compareTo(const K& key) const = 0;
protected:
~ICompareableTo() {}
};

to avoid accidental deletion. But the warnings still hide other, more
important messages.

Is there another way to declare interfaces?
Yes, with the class keyword :-) No, just kidding, of course... there's
no problem in your code. What version of gcc are you using? I have up
to 4.1.1 (built yesterday -hurrah!), and it still emits the warning.
*Perhaps* they fixed that in 4.2 or 4.3 (when the destructor is
protected and the class declares no friends). It's a long-standing
annoyance.

(Off-hand, I think it would require a trivial change in cp/class.c; I
have no idea why they haven't fixed it yet)

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Aug 23 '08 #4
Gennaro Prota wrote:
*Perhaps* they fixed that in 4.2 or 4.3 (when the destructor is
protected and the class declares no friends). It's a long-standing
annoyance.
I was curious and had a look at the SVN repository. It's fixed. They
seem to have first forgotten the protected case, then found the right
form:

<http://gcc.gnu.org/viewcvs/trunk/gcc/cp/class.c?r1=127154&r2=127649>

Revision 127649 means that this (the right form for the protected
case) went into gcc 4.2.2.

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Aug 23 '08 #5
Gennaro Prota wrote:
Gennaro Prota wrote:
>*Perhaps* they fixed that in 4.2 or 4.3 (when the destructor is
protected and the class declares no friends). It's a long-standing
annoyance.

I was curious and had a look at the SVN repository. It's fixed. They
seem to have first forgotten the protected case, then found the right form:

<http://gcc.gnu.org/viewcvs/trunk/gcc/cp/class.c?r1=127154&r2=127649>

Revision 127649 means that this (the right form for the protected case)
went into gcc 4.2.2.
Oh, nice to hear. Unfortunately I am fixed to Gcc 3.3.5, because Gcc 4
is not yet ported for OS/2. So I will seek for a command line switch
that disables exactly this warning without affecting the others, as Pete
recommended.
Marcel
Aug 23 '08 #6
Pete Becker wrote:
On 2008-08-23 08:02:54 -0400, Marcel Müller
<ne**********@spamgourmet.orgsaid:
>If i have an interface like

template <class K>
struct IComparableTo
{ virtual int compareTo(const K& key) const = 0;
};

some compilers (e.g. gcc) warn me about that the class has virtual
functions but a non-virtual destructor. While this can be helpful in
some cases, it can be annoying too.
If the interface is a template like in the example I get hundreds of
warnings for each type instantiation in each compilation unit.

That's what happens when compiler writers assume that they know more
about your application than you do. Surely, though, there's a
command-line switch to turn off this warning.
Well, compiler warnings are meant to catch programmer's oversights.
There's always a trade-off between their helpfulness, risk of false
positives and corresponding compiler complexity. With gcc 4.2.2 (see
my other post) that's IMHO quite a good trade-off.

For the records, you can disable the warning with -Wno-non-virtual-dtor.

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Aug 24 '08 #7

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

Similar topics

0
by: Marc te Vruchte | last post by:
Over the past years i've been in contact with the same problem a number of times, creating a graphical user interface on complex XML documents. Personally these solutions have never been...
11
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
19
by: Duncan McNutt .[FTSE] | last post by:
Why does code derive from for example, IComparer ie, class SomeClass : IComparer { public SomeClass() { }
12
by: Peter N Roth | last post by:
If i build a class in C#, i can declare a method abstract. If someone derives from this class, the abstract method must be overriden for the computation to succeed. ISTM that an Interface...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
27
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software...
18
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
5
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
6
by: S_K | last post by:
Hi, I've been toying around with interfaces in C#. They are fun but can anybody give me some examples of where interfaces are used and what they are used for? Thanks so much. Steve
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.