下面这段是MSDN中对这个宏的描述:
This form of _declspec can be applied to any class declaration, but should only be
applied to pure interface classes, that is classes that will never be instantiated
on their own. The _declspec stops the compiler from generating code to initialize
the vfptr in the constructor(s) and destructor of the class. In many cases, this
removes the only references to the vtable that are associated with the class and,
thus, the linker will remove it. Using this form of _declspec can result in a
significant reduction in code size.
光看这段讲解可能不容易看懂,还是看个具体的例子:
class __declspec(novtable) A
{
public:
A(){}
virtual Test(){}
virtaul Test2(){}
};
class __declspec(novtable) B : public A
{
public:
B(){}
virtual Test(){}
virtual Test3(){Test2();}
};
class C : public B
{
public:
C(){}
};
void MyFunc()
{
C c;
c.Test(); <span class="remark">//没错 </span>
c.Test2();<span class="remark">//没错 </span>
B b;
b.Test3(); <span class="remark">//出错 </span>
}
the ATL_NO_VTABLE macro expands to declspec(novtable). When declspec(novtable) is used in a class declaration, it prevents the vtable pointer from being initialized in the class's constructor and destructor.