Re: some abstract base class dont need vtbl?
petke wrote:
I found an interesting article about why in COM programming such an
optimization is needed http://msdn.microsoft.com/msdnmag/issues/0300/c/
To clarify that, consider how it works in C++, not in assembler.
COM defines interfaces; ATL provides their partial implementations,
as in the following example:
#define NOVTABLE __declspec(novtable)
// interface defined by COM
class NOVTABLE IFoo {
public:
virtual void foo() = 0;
};
// partial implementation provided by ATL
template<class T>
class NOVTABLE IFooImpl : public IFoo {
public:
virtual void foo()
{
T* pT = static_cast<T*>(this);
pT->do_foo();
}
void do_foo()
{
// derived do_foo will be called, if any
}
};
// My own class that exposes some COM interface(s)
class C : public IFooImpl<C> {
public:
void do_foo()
{
// do some useful foo
}
};
IFoo* create_C() // Class factory for my class
{
return new C();
}
Yes, there is no need to have vtables for IFoo and IFooImpl, because
they are never instantiated by themselves, but only as part of derived
class(es).
But should I specify NOVTABLE explicitly?
1. IFoo has trivial constructors and destructor, and I am unable
to instantiate it explicitly (it is abstract). There is no chance
for me to dereference its vtable - it's safe to optimize it away.
2. IFooImpl has trivial constructors and destructor too, and compiler
never exposes me instantiating IFooImpl explicitly. It is
instantiated only as part of derived class, but there is still no
chance to dereference its vtable - it's safe to optimize it away.
Both cases are optimizeable, there is no need for __declspec(novtable).
There is a need for better optimizers.
--
Andrei Polushin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]