- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
或者,使用 __declspec(novtable) 是否有任何其他已知的负面影响?我似乎找不到任何问题的引用。
最佳答案
MSCV 使用每个对象一个 vptr,每个类一个 vtbl
来实现 OO 机制,例如 RTTI 和虚函数。
因此,当且仅当 vptr 已正确设置时,RTTI 和虚函数才能正常工作。
struct __declspec(novtable) B {
virtual void f() = 0;
};
struct D1 : B {
D1() {
} // after the construction of D1, vptr will be set to vtbl of D1.
};
D1 d1; // after d has been fully constructed, vptr is correct.
B& b = d1; // so virtual functions and RTTI will work.
b.f(); // calls D1::f();
assert( dynamic_cast<D1*>(&b) );
assert( typeid(b) == typeid(D1) );
使用__declspec(novtable)
时B应该是一个抽象类。
除了 D1 的构造函数之外,不会有 B 的实例。
而 __declspec(novtable) 在大多数情况下没有负面影响。
但是在派生类的构造过程中,__declspec(novtable)
会使其不同于ISO C++语义。
struct D2 : B {
D2() { // when enter the constructor of D2 \
// the vtpr must be set to vptr of B \
// if B didn't use __declspec(novtable).
// virtual functions and RTTI will also work.
this->f(); // should calls B::f();
assert( typeid(*this) == typeid(B) );
assert( !dynamic_cast<D2*>(this) );
assert( dynamic_cast<B*>(this) );
// but __declspec(novtable) will stop the compiler \
// from generating code to initialize the vptr.
// so the code above will crash because of uninitialized vptr.
}
};
注意:虚拟 f() = 0
;让 f 成为一个纯虚函数
,让 B 成为一个抽象类。
纯虚函数的定义
可以
(不是必须
)缺失。
C++ 允许在我们不推荐的构造函数中调用虚函数。
更新:D2 中的错误:派生构造函数中的 vptr。
struct D3 : B { // ISO C++ semantic
D3() { // vptr must be set to vtbl of B before enter
} // vptr must be set to vtbl of D2 after leave
};
但是vptr在构造过程中是不确定的,这也是不推荐在构造函数中调用虚函数的原因之一。
如果 D2::D2() 中的 vptr 是 B 并且缺少 B::f() 的定义,this->f();
将在取消引用函数指针时崩溃vtbl.
如果 D2::D2() 中的 vptr 是 B 并且 B 使用 novtable,this->f();
将在取消引用未初始化的 vptr 时崩溃。
事实上,D2::D2()中的vptr是MSVC(msvc8)中的D2。编译器在执行D2::D2()中的其他代码之前将vptr设置为D2。
所以 this->f();
调用 D2::f() 并且三个断言将被违反。
关于c++ - 在抽象基类上使用 __declspec(novtable) 是否会以任何方式影响 RTTI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1801258/
__declspec(restrict) 和 __declspec(noalias) 有什么区别我已阅读此页 https://msdn.microsoft.com/en-us/library/k649
我正在尝试使用 macOS mojave 上的终端从 c++ 使用 NDK 将函数导出到共享对象 (.so)。我已经安装了最新版本的命令行工具和 Xcode。 这是我要导出的 GetNumber()
在 C++ 中,我对将指针变量声明为只读感兴趣,我正在考虑通过以下机制来实现: #pragma section (".readonly", read) __declspec(allocate(".re
我应该使用 bool __declspec(dllexport) function() { return true; } 或 __declspec(dllexport) bool functi
代码: #ifdef BUILD_DLL #define MY_API __declspec(dllexport) #else #define MY_API __declspec(dllimport)
您好,我对 dllexport 有点困惑。例如,当我在类里面使用 __declspec( dllexport ) 时 #define DllExport __declspec( dllexpor
我试图了解动态链接的工作原理,并且我了解其中的大部分内容,但是现在编译器或链接器如何知道我究竟从哪个 dll 导入? 例如我有 test_program.dll导出一个名为 test(); 的函数我用
基于 MSDN,__declspec(align(x)) 应该在成员变量之后添加 x 位填充,例如: #include using namespace std; void main() {
我试图通过导出为 DLL(在 Windows/VS 2010 上)来保护一些 C++ 代码。 在下面的示例中,var 在父类(super class)构造函数中设置,并且调试器显示它肯定设置为引用某物
如果我有 SOME_MACRO 定义为 __declspec(dllimport) 或 __declspec(dllexport),有没有办法检查编译时用的是哪一个? 即像这样: #if SOME_M
我们知道,novtable 表示不为纯抽象类创建虚表。但是当我运行代码时,出现了错误: #include using namespace std; struct A{ virtual voi
是的,我已经阅读了这个:http://msdn.microsoft.com/en-us/library/83ythb65.aspx但我不清楚。首先,__declspec(align(#)) 使用它声明
我在看这个:Importing Function Calls Using __declspec(dllimport)而且我不明白为什么真的需要 __declspec(dllimport)?为什么链接器
我想在我的一些返回对象引用的成员函数上应用 __declspec(nothrow)。例如,我想将它添加到此函数(在 MyClass.h 中): CMyClass& operator= ( IN UIn
我想构建 2 个 dll,我们称它们为 Foo 和 Bar。我希望 Bar 从 Foo 导入一些类。 Foo.h: #ifdef EXPORT #define DECL __declspec(dlle
为什么会出现以下行为?这是错误还是正常行为? (已使用 Visual Studio 2013 和 2017 进行检查)似乎使用虚函数作为 getter 或 setter 可能无法按预期工作! clas
很抱歉,这个问题非常简单,无法通过 google 找到答案。 这个声明语法是: __declspec(align(16)) float rF[4]; __declspec(align(16)) flo
我正在研究库的多线程实现。在这个库的一个模块中有一些全局变量(在程序执行中经常使用)。为了使对这些变量的访问更加安全,我使用线程本地存储 (TLS) 关键字 __declspec(thread) 来声
我正在考虑将为 Windows 编写的脚本引擎移植到 Linux;它适用于 Winamp 的可视化平台 AVS。我不确定目前是否有可能。据我所知,代码正在获取 C 函数 nseel_asm_atan
http://msdn.microsoft.com/en-us/library/9h658af8.aspx MSDN 说我可以使用 __declspec(dllexport) 从库中导出函数,但是如何
我是一名优秀的程序员,十分优秀!