"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?
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
|