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

inline virtual destructor in a header file??

My class in a header file, contains inline virtual destructor.
Is this Ok? Can it cause any problems?

class base
{
public:
base() { }
virtual ~base { std::cout<<"Inside virtual destructor\n"; }

// Other members
};
Jul 22 '05 #1
20 5918

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
My class in a header file, contains inline virtual destructor.
Is this Ok? Can it cause any problems?

class base
{
public:
base() { }
virtual ~base { std::cout<<"Inside virtual destructor\n"; }

// Other members
};


It's fine, in fact its common. Why shouldn't it be OK?

john
Jul 22 '05 #2


qazmlp wrote:
My class in a header file, contains inline virtual destructor.
Is this Ok? Can it cause any problems?

class base
{
public:
base() { }
virtual ~base { std::cout<<"Inside virtual destructor\n"; }

// Other members
};

If you declare a function as inline it doesn't mean that the compiler
*has* to inline that code - which usually wouldn't work with virtual
functions - you just 'suggest' that he *could* inline it. So it's all
right with that...

Jul 22 '05 #3
qazmlp wrote:
My class in a header file, contains inline virtual destructor.
Is this Ok? Can it cause any problems?


It can't. What excactly makes you to worry about it?

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
My class in a header file, contains inline virtual destructor.
Is this Ok? Can it cause any problems?

class base
{
public:
base() { }
virtual ~base { std::cout<<"Inside virtual destructor\n"; }

// Other members
};


It's fine, in fact its common. Why shouldn't it be OK?


Because it's "inline" and it's also "virtual". You don't see why that might
be contradictory and confusing?
Jul 22 '05 #5
jeffc wrote:
> My class in a header file, contains inline virtual destructor.
> Is this Ok? Can it cause any problems?
>
> class base
> {
> public:
> base() { }
> virtual ~base { std::cout<<"Inside virtual destructor\n"; }
>
> // Other members
> };


It's fine, in fact its common. Why shouldn't it be OK?


Because it's "inline" and it's also "virtual". You don't see why that might
be contradictory and confusing?
...


It shouldn't be contradictory and/or confusing to a person who fully
understands the meaning of these qualifiers in C++.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #6
"jeffc" <no****@nowhere.com> wrote
My class in a header file, contains inline virtual destructor.
Is this Ok? Can it cause any problems?

class base
{
public:
base() { }
virtual ~base { std::cout<<"Inside virtual destructor\n"; }

// Other members
};


It's fine, in fact its common. Why shouldn't it be OK?


Because it's "inline" and it's also "virtual". You don't see why that might
be contradictory and confusing?


Virtual functions can be inlined. The compiler can determine by context whether
to inline the code or do a polymorphic call. For example, if you invoke a
virtual method directly on an object (not a reference or a pointer), the
compiler MAY choose to inline the code, since there's no ambiguity. Or it can
make a direct (non-vtable) call to the out-of-line function. If you're calling
the same virtual function through a pointer or a reference, the compiler must
then select the appropriate function through the vtable. This implies that you
may (and probably will) have both an out-of-line and inline implementations of
your function if (a) it's inlineable, and (b) you call it directly at some
point.

A detail to remember: when you invoke a method from within the object, it's
invoked through the 'this' pointer, which means that unless you qualify it, you
always get a polymorphic call.

Claudio Puviani
Jul 22 '05 #7

"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:Jd********************@comcast.com...
jeffc wrote:
> My class in a header file, contains inline virtual destructor.
> Is this Ok? Can it cause any problems?
>
> class base
> {
> public:
> base() { }
> virtual ~base { std::cout<<"Inside virtual destructor\n"; }
>
> // Other members
> };

It's fine, in fact its common. Why shouldn't it be OK?


Because it's "inline" and it's also "virtual". You don't see why that might be contradictory and confusing?
...


It shouldn't be contradictory and/or confusing to a person who fully
understands the meaning of these qualifiers in C++.


It seems to be a very common misunderstanding that the meaning of the
keyword inline has something to do with inlining function calls (easy to see
how that misunderstanding would occur). However the only meaning that the
C++ standard gives to inline functions is as an exception to the one
definition rule, thereby allowing inline functions to usefully appear in
header files.

In any case there are cases where a virtual function is called via a
non-virtual mechanism, this is especially true of a destructor.

john
Jul 22 '05 #8

"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:Jd********************@comcast.com...

It's fine, in fact its common. Why shouldn't it be OK?


Because it's "inline" and it's also "virtual". You don't see why that might be contradictory and confusing?
...


It shouldn't be contradictory and/or confusing to a person who fully
understands the meaning of these qualifiers in C++.


Use your head man - does the originator of this thread sound like someone
who "fully understands the meaning of these qualifiers in C++"?
Jul 22 '05 #9

"John Harrison" <jo*************@hotmail.com> wrote:
It seems to be a very common misunderstanding that the meaning of the keyword inline has something to do with inlining function calls (easy to see how that misunderstanding would occur). However the only meaning that the C++ standard gives to inline functions is as an exception to the one
definition rule, thereby allowing inline functions to usefully appear in header files.


It does have 'something to do' with inlining function calls:

"The inline specifier indicates to the implementation that inline
substitution of the function body at the point of call is to be
preferred to the usual function call mechanism." (7.1.2)

Inlining is not required, as you know, but it's a bit of an
overstatement to say that 'inline' is completely unrelated to
inlining.

Jonathan

Jul 22 '05 #10
Claudio Puviani wrote:
"jeffc" <no****@nowhere.com> wrote
My class in a header file, contains inline virtual destructor.
Is this Ok? Can it cause any problems?

class base
{
public:
base() { }
virtual ~base { std::cout<<"Inside virtual destructor\n"; }

// Other members
};

It's fine, in fact its common. Why shouldn't it be OK?


Because it's "inline" and it's also "virtual". You don't see why that might
be contradictory and confusing?

Virtual functions can be inlined. The compiler can determine by context whether
to inline the code or do a polymorphic call. For example, if you invoke a
virtual method directly on an object (not a reference or a pointer), the
compiler MAY choose to inline the code, since there's no ambiguity.


Inline destructors, whether virtual or not, can be a cause
of code bloat:

#include <string>
#include <vector>

using namespace std;

class A {
string name;
string address;
string telephone;
public:
A() {};
virtual void function() {}
virtual ~A() {}
};

class B : public A {
public:
B() {};
void function() {}
~B() {}
};

void abc() {
B b;
b.function();
}

int main()
{
B b;
b.function();
abc();
return 0;
}

check the assembler output from the above, and compare it to
the output where the destructors are defined outline.
Jul 22 '05 #11

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c om...
My class in a header file, contains inline virtual destructor.
Is this Ok? Can it cause any problems?

class base
{
public:
base() { }
virtual ~base { std::cout<<"Inside virtual destructor\n"; }

// Other members
};


1. don't try to inline stuff that is not trivial - template I/O is never
trivial - it's a horrendous mess of
inlined template stuff with conditions loops, exceptions etc. that the
compiler is virtually certain
do decide is way too hard to inline.
2. Apart from that inlining virtual dtors is a good idea if they are
trivial -
Every derived class dtor whether explicit or implicit will call the base
class dtor - if this can be inlined then
this should result in saving a function call.
3. The only other problem for trivial dtors is if the base class is ever
instantiated - If it is it will need a vtable
which will need an outline dtor. If you add a pure virtual method (the
dtor can be pure even if defined)
then the compiler should realise that the class cannot be instantiated
and
hence it will never need a vtable and hence never need an outlined dtor.
4. your compiler might not be that smart.
Jul 22 '05 #12
"lilburne" <li******@godzilla.net> wrote
Inline destructors, whether virtual or not, can be a cause
of code bloat:


Any inline function -- destructor or otherwise -- can be a source of code bloat
if the inlining wasn't chosen judiciously. In the example you gave, the bloat
comes from inlining a destructor that implicitly called three other destructors.
The problem wasn't that _a_ destructor was inlined, but that that particular
destructor shouldn't have been inlined if code size had been your criterion for
inlining.

To counterbalance your example, an inlined trivial destructor could be
completely optimized out, whereas the same trivial destructor out-of-line would
still cause invocation code to be generated everywhere an object of that type is
destroyed.

Code bloat, bad performance, sub-optimal memory usage... those are all results
of bad programming, not of using a specific language feature.

Claudio Puviani
Jul 22 '05 #13

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:c1*************@ID-216073.news.uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote:
It seems to be a very common misunderstanding that the meaning of

the
keyword inline has something to do with inlining function calls

(easy to see
how that misunderstanding would occur). However the only meaning

that the
C++ standard gives to inline functions is as an exception to the one
definition rule, thereby allowing inline functions to usefully

appear in
header files.


It does have 'something to do' with inlining function calls:

"The inline specifier indicates to the implementation that inline
substitution of the function body at the point of call is to be
preferred to the usual function call mechanism." (7.1.2)

Inlining is not required, as you know, but it's a bit of an
overstatement to say that 'inline' is completely unrelated to
inlining.

Jonathan


I chose my words carefully (perhaps not carefully enough). The point I was
trying to make was that the meaning of a program is unaffected by whether a
function call is inlined or not, also a programs legality is unaffected by
whether a function call is inlined or not. That's why I said the *meaning*
of the keyword inline. I wasn't trying to say that inline is completely
unrelated to function call inlining.

john
Jul 22 '05 #14

"John Harrison" <jo*************@hotmail.com> wrote in message:
I chose my words carefully (perhaps not carefully enough). The point I was trying to make was that the meaning of a program is unaffected by whether a function call is inlined or not, also a programs legality is unaffected by whether a function call is inlined or not. That's why I said the *meaning* of the keyword inline. I wasn't trying to say that inline is completely unrelated to function call inlining.


Okay -- I'll resist the tempatation to argue about the meaning of
'meaning'. ;-)

Jonathan
Jul 22 '05 #15

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:c1*************@ID-216073.news.uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message:
I chose my words carefully (perhaps not carefully enough). The point

I was
trying to make was that the meaning of a program is unaffected by

whether a
function call is inlined or not, also a programs legality is

unaffected by
whether a function call is inlined or not. That's why I said the

*meaning*
of the keyword inline. I wasn't trying to say that inline is

completely
unrelated to function call inlining.


Okay -- I'll resist the tempatation to argue about the meaning of
'meaning'. ;-)

Jonathan


The word 'meaning' has only one meaning, I refer you to 1.9.1 of the C++
standard, especially footnote 5.

john
Jul 22 '05 #16

"John Harrison" <jo*************@hotmail.com> wrote:
"Jonathan Turkanis" <te******@kangaroologic.com> wrote:
Okay -- I'll resist the tempatation to argue about the meaning of
'meaning'. ;-)


<snip>
The word 'meaning' has only one meaning, I refer you to 1.9.1 of the C++ standard, especially footnote 5.


Search the standard for 'meaning'. You'll find that even in
standardese, 'meaning' has many meanings.

But I said I wouldn't argue.

Jonathan
Jul 22 '05 #17
Claudio Puviani wrote:
"lilburne" <li******@godzilla.net> wrote

Code bloat, bad performance, sub-optimal memory usage... those are all results
of bad programming, not of using a specific language feature.


I didn't say that it was anything to do with a particular
language feature. The point was that what might look like a
trivial piece a code (a destructor that looks like it has no
body) sometimes isn't.

If the class contains nothing but PODs there is no overhead.
Once you have a class that contains a member that requires a
non-trivial dtor you'll have the problem illustrated, which
may be added in 6 months time. How many newbies do you think
would be aware of the overhead of such destructors?

Jul 22 '05 #18
Andrey Tarasevich <an**************@hotmail.com> wrote in message news:<s8********************@comcast.com>...
qazmlp wrote:
My class in a header file, contains inline virtual destructor.
Is this Ok? Can it cause any problems?


It can't. What excactly makes you to worry about it?


As I remember, inline and virtual cannot co-exist.
So, I wanted to know whether there will be a problem, if the compiler
makes the virtual destructor as inline.

As the reply posts have confirmed that there is no problem, it is
fine.

Thanks!
Jul 22 '05 #19
>
But I said I wouldn't argue.


Shame, it's not often we get the chance to debate philosophy in
comp.lang.c++.

john
Jul 22 '05 #20
It is ok.

Jul 22 '05 #21

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

Similar topics

15
by: Dave Townsend | last post by:
Yo, I had a job interview today, the interviewing asked me about inline virtual functions, or what was my opinion on them. Hm, I've seen mention of these babies in the reference material, but...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
8
by: John Ratliff | last post by:
Can the compiler ever inline a method when there is a pointer to the member used? Thanks, --John Ratliff
10
by: mark | last post by:
I have this class: class Selections { OSStatus Init(); protected: CFMutableSetRef selectionObjects; static void CFASelectionsApplier(const void* value, void* ctx); OSType ready; public:...
6
by: Alden Pierre | last post by:
Hello, http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 As per the link above it's wise to have a virtual deconstructor when creating an abstract class. Here is when I'm...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
8
by: siddhu | last post by:
Dear experts, A virtual function has to have an address. So if an inline virtual function is actually inlined then in that case what does address of this function signify? How does compiler know...
7
by: Tonni Tielens | last post by:
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...
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
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,...
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,...
0
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...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.