ATL中的ATL_NO_VTABLE宏

ATL_NO_VTABLE可以让编译器不产生VTable,并且不设置VPointer的值。
正常情况下,在调用构造函数和析构函数的时候,编译器都会插入相应的代码。如果ATL_NO_VTABLE,可以省去这些代码。ATL中使用这个,是因为这个类为抽象类,不需要VTable。真正的类是CComObject,它继承用户的类。


--------------------------------------------------------------

#define ATL_NO_VTABLE __declspec(novtable)

class ATL_NO_VTABLE MyBase
{
public:
	MyBase()
	{
		cout << "MyBase::MyBase()" << endl;
	}
	virtual ~MyBase()
	{
		cout << "MyBase::~MyBase()" << endl;
	}
};
class Derive :public MyBase
{
public:
	Derive()
	{
		cout << "Derive::Derive()" << endl;
	}
	virtual ~Derive()
	{
		cout << "Derive::~Derive()" << endl;
	}
};


int main(int argc, char* argv[])
{

	MyBase* p1 = new Derive;
	delete p1;

	cout << "==========================" << endl;
	
	MyBase* p2 = new MyBase();
	delete p2;//运行时出错

	return 0;
}


--------------------------------------------------------------

转载于:https://www.cnblogs.com/fanzi2009/archive/2010/04/16/1713292.html

你可能感兴趣的:(ATL中的ATL_NO_VTABLE宏)