Re: some abstract base class dont need vtbl?
Yeah, I give in. The compiler should be able to tell. Nice example.
template<class T>
class IFooImpl : public IFoo {
...
virtual void foo()
{
T* pT = static_cast<T*>(this);
pT->do_foo();
}
...
class C : public IFooImpl<C> {
...
That is really nice. Compiletime virtuals. I had never seen it before
looking into COM programming, and I have not seen it used outside of
COM since. Was it just me or do people not know about this useful
trick?
Well back to the question..
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.
I am wondering about this. Wouldnt the compiler also have to make sure
that no derived object is ever deleted thru a base pointer?
class C : public IFooImpl<C> {
...
IFoo* create_C() // Class factory for my class
{
return new C();
}
As in, wouldnt this part cause problems when the new C object would be
deleted thru a base pointer? Well C does not contain any member
variables but surely it must take some space that needs to be
deconstructed? Or am I missing somethin?
cheers Petke
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]