Connecting Tech Pros Worldwide Help | Site Map

inline virtual destructor in a header file??

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 06:47 AM
qazmlp
Guest
 
Posts: n/a
Default 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
};

  #2  
Old July 22nd, 2005, 06:47 AM
John Harrison
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??


"qazmlp" <qazmlp1209@rediffmail.com> wrote in message
news:db9bbf31.0402222322.6c55d1a7@posting.google.c om...[color=blue]
> 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
> };[/color]

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

john


  #3  
Old July 22nd, 2005, 06:47 AM
Patrik Stellmann
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??



qazmlp wrote:[color=blue]
> 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
> };[/color]
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...

  #4  
Old July 22nd, 2005, 06:47 AM
Andrey Tarasevich
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??

qazmlp wrote:[color=blue]
> My class in a header file, contains inline virtual destructor.
> Is this Ok? Can it cause any problems?[/color]

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

--
Best regards,
Andrey Tarasevich

  #5  
Old July 22nd, 2005, 06:47 AM
jeffc
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??


"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:c1caf7$1gg5ad$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "qazmlp" <qazmlp1209@rediffmail.com> wrote in message
> news:db9bbf31.0402222322.6c55d1a7@posting.google.c om...[color=green]
> > 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
> > };[/color]
>
> It's fine, in fact its common. Why shouldn't it be OK?[/color]

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


  #6  
Old July 22nd, 2005, 06:47 AM
Andrey Tarasevich
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??

jeffc wrote:[color=blue][color=green][color=darkred]
>> > 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
>> > };[/color]
>>
>> It's fine, in fact its common. Why shouldn't it be OK?[/color]
>
> Because it's "inline" and it's also "virtual". You don't see why that might
> be contradictory and confusing?
> ...[/color]

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

  #7  
Old July 22nd, 2005, 06:47 AM
Claudio Puviani
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??

"jeffc" <nobody@nowhere.com> wrote[color=blue][color=green][color=darkred]
> > > 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
> > > };[/color]
> >
> > It's fine, in fact its common. Why shouldn't it be OK?[/color]
>
> Because it's "inline" and it's also "virtual". You don't see why that might
> be contradictory and confusing?[/color]

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


  #8  
Old July 22nd, 2005, 06:47 AM
John Harrison
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??


"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote in message
news:JdWdnTtGusNqrafd4p2dnA@comcast.com...[color=blue]
> jeffc wrote:[color=green][color=darkred]
> >> > 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?[/color]
> >
> > Because it's "inline" and it's also "virtual". You don't see why that[/color][/color]
might[color=blue][color=green]
> > be contradictory and confusing?
> > ...[/color]
>
> It shouldn't be contradictory and/or confusing to a person who fully
> understands the meaning of these qualifiers in C++.
>[/color]

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


  #9  
Old July 22nd, 2005, 06:48 AM
jeffc
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??


"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote in message
news:JdWdnTtGusNqrafd4p2dnA@comcast.com...[color=blue][color=green][color=darkred]
> >>
> >> It's fine, in fact its common. Why shouldn't it be OK?[/color]
> >
> > Because it's "inline" and it's also "virtual". You don't see why that[/color][/color]
might[color=blue][color=green]
> > be contradictory and confusing?
> > ...[/color]
>
> It shouldn't be contradictory and/or confusing to a person who fully
> understands the meaning of these qualifiers in C++.[/color]

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


  #10  
Old July 22nd, 2005, 06:48 AM
Jonathan Turkanis
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??


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

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



  #11  
Old July 22nd, 2005, 06:48 AM
lilburne
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??

Claudio Puviani wrote:
[color=blue]
> "jeffc" <nobody@nowhere.com> wrote
>[color=green][color=darkred]
>>>>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?[/color]
>>
>>Because it's "inline" and it's also "virtual". You don't see why that might
>>be contradictory and confusing?[/color]
>
>
> 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.[/color]

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.


  #12  
Old July 22nd, 2005, 06:48 AM
Nick Hounsome
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??


"qazmlp" <qazmlp1209@rediffmail.com> wrote in message
news:db9bbf31.0402222322.6c55d1a7@posting.google.c om...[color=blue]
> 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
> };[/color]

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.


  #13  
Old July 22nd, 2005, 06:48 AM
Claudio Puviani
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??

"lilburne" <lilburne@godzilla.net> wrote[color=blue]
> Inline destructors, whether virtual or not, can be a cause
> of code bloat:[/color]

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


  #14  
Old July 22nd, 2005, 06:48 AM
John Harrison
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??


"Jonathan Turkanis" <technews@kangaroologic.com> wrote in message
news:c1dnsc$1h9fgd$1@ID-216073.news.uni-berlin.de...[color=blue]
>
> "John Harrison" <john_andronicus@hotmail.com> wrote:
>[color=green]
> > It seems to be a very common misunderstanding that the meaning of[/color]
> the[color=green]
> > keyword inline has something to do with inlining function calls[/color]
> (easy to see[color=green]
> > how that misunderstanding would occur). However the only meaning[/color]
> that the[color=green]
> > C++ standard gives to inline functions is as an exception to the one
> > definition rule, thereby allowing inline functions to usefully[/color]
> appear in[color=green]
> > header files.[/color]
>
> 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
>[/color]

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


  #15  
Old July 22nd, 2005, 06:48 AM
Jonathan Turkanis
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??


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

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

Jonathan


  #16  
Old July 22nd, 2005, 06:48 AM
John Harrison
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??


"Jonathan Turkanis" <technews@kangaroologic.com> wrote in message
news:c1dtnq$1ho8re$1@ID-216073.news.uni-berlin.de...[color=blue]
>
> "John Harrison" <john_andronicus@hotmail.com> wrote in message:
>[color=green]
> > I chose my words carefully (perhaps not carefully enough). The point[/color]
> I was[color=green]
> > trying to make was that the meaning of a program is unaffected by[/color]
> whether a[color=green]
> > function call is inlined or not, also a programs legality is[/color]
> unaffected by[color=green]
> > whether a function call is inlined or not. That's why I said the[/color]
> *meaning*[color=green]
> > of the keyword inline. I wasn't trying to say that inline is[/color]
> completely[color=green]
> > unrelated to function call inlining.[/color]
>
> Okay -- I'll resist the tempatation to argue about the meaning of
> 'meaning'. ;-)
>
> Jonathan
>[/color]

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

john


  #17  
Old July 22nd, 2005, 06:48 AM
Jonathan Turkanis
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??


"John Harrison" <john_andronicus@hotmail.com> wrote:[color=blue]
> "Jonathan Turkanis" <technews@kangaroologic.com> wrote:[/color]
[color=blue][color=green]
> > Okay -- I'll resist the tempatation to argue about the meaning of
> > 'meaning'. ;-)[/color][/color]

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

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

But I said I wouldn't argue.

Jonathan


  #18  
Old July 22nd, 2005, 06:48 AM
lilburne
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??

Claudio Puviani wrote:[color=blue]
> "lilburne" <lilburne@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.
>[/color]

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?

  #19  
Old July 22nd, 2005, 06:48 AM
qazmlp
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??

Andrey Tarasevich <andreytarasevich@hotmail.com> wrote in message news:<s8udnXFsD7ovJaTdRVn-iQ@comcast.com>...[color=blue]
> qazmlp wrote:[color=green]
> > My class in a header file, contains inline virtual destructor.
> > Is this Ok? Can it cause any problems?[/color]
>
> It can't. What excactly makes you to worry about it?[/color]

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!
  #20  
Old July 22nd, 2005, 06:49 AM
John Harrison
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??

>[color=blue]
> But I said I wouldn't argue.
>[/color]

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

john


  #21  
Old July 22nd, 2005, 07:07 AM
Peter Jansson
Guest
 
Posts: n/a
Default Re: inline virtual destructor in a header file??

It is ok.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.