惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
博客园_首页
博客园 - 【当耐特】
V
Visual Studio Blog
博客园 - 叶小钗
月光博客
月光博客
美团技术团队
J
Java Code Geeks
小众软件
小众软件
Y
Y Combinator Blog
博客园 - Franky
Martin Fowler
Martin Fowler
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
Microsoft Security Blog
Microsoft Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG

博客园 - ->

[Under the hood]---Matt Pietrek October 1996 MSJ [under the hood]Reduce EXE and DLL Size with LIBCTINY.LIB TN035: Using Multiple Resource Files and Header Files with Visual C++ The quick brown fox jumps over the lazy dog. [转]IOCP介绍 A simple IOCP Server/Client Class Flash for Linux CodeProject站点地图 MFC Macros and Globals (搜集)一些少走弯路的话语+参考信息 [总结]软件工程师笔试题目(C++) [转贴][VC++6.0] 一个很深的模板Bug [转贴]35岁之前成功12条法则 C++/STL/VC资源链接(查找方便) [转贴]The Code Project Visual C++ Forum FAQ 看看你是否需要更新SYMBOL文件了?? 世界上有很多的"绳"模型 [转贴]五个寓言故事令你受益匪浅 为什么用户自定义消息通常+0x400
[转贴]VC:__declspec(novtable)
-> · 2006-02-25 · via 博客园 - ->

原文地址:http://blog.csdn.net/toby/archive/2004/10/24/149366.aspx
我怎么又比别人晚了进2年的时间,后面我会重新写一篇文章,算是对该文的整理吧!

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版本的尺寸小的原因之一吧。