其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
VC:__declspec(novtable)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

C++里virtual的缺陷就是vtable会增大代码的尺寸,看vcl时,object pascal里virtual也有vtable的问题,于是又了dynamic,两种方法各有利弊。但是在C++里却没有这样的机制,原来也没深想过,今天看MFC代码时,在CObject的定义时:
class AFX_NOVTABLE CObject
{
...
}

AFX_NOVTABLE是什么东东?是个宏,在Afxver_.h中:
#if _MSC_VER >= 1100 && !defined(_DEBUG)
#define AFX_NOVTABLE __declspec(novtable)
#else
#define AFX_NOVTABLE
#endif

也就是说在你编译Release版本时,在CObject前是__declspec(novtable),在debug版本没有这个限制。MSDN里的解释是:
-----------------------------------------------------------------

Microsoft Specific

This is a __declspec extended attribute.

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.

If you attempt to instantiate a class marked with novtable and then access a class member, you will receive an access violation (AV).

Example

// novtable.cpp
#include <stdio.h>
class __declspec(novtable) X
{
public:
   virtual void mf();
};

class Y : public X
{
public:
   void mf()
   {
      printf("In Y\n");
   }
};

int main()
{
   // X *pX = new X();
   // pX->mf();   // AV at runtime
   Y *pY = new Y();
   pY->mf();
}

Output

In Y

END Microsoft Specific
-----------------------------------------------------------------
依照AFX_NOVTABLE的声明,对CObject在debug模式,是不起作用的,而在release模式时将移除CObject的vtable,这是release比debug版本的尺寸小的原因之一吧。




相关文章

相关软件