* Pete C:
In this section of the FAQ:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.10
an abstract destructor is given a definition with the cryptic comment
"It's faster this way, trust me". Why?
Is it faster to type?
Since the definition is provided 'inline' a compiler that doesn't do whole-
program optimization can easily optimize that particular destructor call
away in each derived class' destructor, thus, it's probably a little bit
faster than not inline. The destructor is there and is virtual because this
class is designed for polymorphic usage.
I notice that the derived classes in that example
seem mysteriously not to require a destructor.
Actually Funct2 and Funct3 do, and the compiler generates it, and the base
class is designed to support such derived classes.
Or somehow faster at runtime? What purpose is served by the destructor
being abstract?
Why it's pure virtual I don't know but it doesn't hurt. For a large class it
can be a time-saving convention to make the destructor pure virtual if the
class is abstract, so you only have to look at the destructor to ascertain
whether it is abstract or not. Essentially a convention that gives
at-a-glance effect of Java or C# 'abstract' keyword -- but I wouldn't rely
on such a convention, and I don't think it adds much useful information.
What baffles me is nothing of the above, but why Marshall has chosen to use
the term 'functionoid' instead of the well established (for C++) 'functor'.
However, Google helped me quickly find Marshall's own explanation of that
choice, at <url:
http://www.kegel.com/dkftpbench/callbackDemo.html>:
<quote>
A functionoid is an
object that has one major method. It's basically the OO extension of a
C-like function such as printf(). One would use a functionoid whenever the
function has more than one entry point (i.e., more than one "method") and/or
needs to maintain state between calls in a thread-safe manner (the C-style
approach to maintaining state between calls is to add a local "static"
variable to the function, but that is horribly unsafe in a multi-threaded
environment).
A functor is a special case of a functionoid: it is a functionoid whose
method is the "function-call operator," operator()(). Since it overloads
the function-call operator, code can call its major method using the same
syntax they would for a function call. E.g., if "foo" is a functor, to call
the "operator()()" method on the "foo" object one would say "foo()". The
benefit of this is in templates, since then the template can have a template
parameter that will be used as a function, and this parameter can be either
the name of a function or a functor-object. There is a performance
advantage of it being a functor object since the "operator()()" method can
be inlined (whereas if you pass the address of a function it must,
necessarily, be non-inlined).
</quote>
Cheers,
- Alf