首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

虚函数表碰到了_declspec(novtable)

2012-10-18 
虚函数表遇到了__declspec(novtable)C/C++ code例子 1class __declspec(novtable) A{public:virtual void

虚函数表遇到了__declspec(novtable)

C/C++ code
例子 1class __declspec(novtable) A{public:virtual void funA(){};    };classs C:    public A        //C中有没有虚函数表? 肯定有吧, __declspec(novtable)能干啥?{};例子 2class __declspec(novtable) A{public:virtual void funA(){};    };classs B{public:virtual void funB(){};};class D :public  A, public B            //此时呢? 虚函数表有如何呢?{virtual void funB(){int i=2;};};


[解决办法]
另外,5楼的同学.不知道你从哪儿看的novtable是让编译器静态绑定的...搞我的以为我记错了..
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).

微软的标准里说,这玩意就是为了消除抽象类的一些代码的.
扩展不可能去改变标准的行为.最多是添加标准里没有的东西...

[解决办法]
探讨

2 位的意思是:

加上这个关键字,那么 就 是一个抽象类了,

既然是抽象类,就不能实现代码,一旦实现代码, 编译器就出错?

事实可以证明,这个关键字并非一定就是抽象类,只是可能是。

发code:

class _declspec(novtable) A
{
public:
virtual void fun()=0;
void fun2(){}
};

……

热点排行