指向 VS2015 Update 2 中非静态数据成员的指针不正确
Pointer to a non-static data members incorrect in VS2015 Update 2
VS2015 Update 2 中的编译器似乎C++回归 - 当对具有虚拟析构函数的类之一使用多重继承时,计算基类数据成员的不正确添加。代码示例:
// Example program
#include <iostream>
#include <limits>
struct V
{
virtual ~V() {};
};
struct B
{
int i;
};
struct A : public B, public V
{
};
int main()
{
int A::* a1 = &A::i;
A a;
a.i = std::numeric_limits<int>::max();
if (a.*a1 == std::numeric_limits<int>::max())
std::cout << "okn";
else
std::cout << "nokn";
}
看起来 a1 得到了对向表的偏移量而不是 i。
此代码用于在VS2015中打印正常,直到更新1和更新2中的nok。我在GCC和Clang打印的支票也可以。
使用指向成员的指针时,多重继承是否有任何限制?
回答我自己的问题,以防有人遇到同样的问题:更新 2 中确实存在回归,正如 MSDN 论坛上所讨论的那样。基本上你有两个选择:
- 使用
/vmg
编译器开关(这将增加二进制文件的大小)。 - 通过基类
&B::i
获取正确的偏移量,并将其分配给派生类A
相关文章:
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 使用指针访问数组中的对象数据成员
- 共享 C++ 的数据成员指针
- C++数据成员:值与指针
- 如何使用数据成员填充派生类的对象到基类的指针数组中
- 如何在C++中使用类对象访问指针数据成员
- 将指向数据成员的指针传递给 std::invoke 时有哪些用例和有用性?
- 通过指针算法访问结构数据成员
- 如何强制实施有关指针数据成员的常量正确性
- 类数据成员指针的非类型模板参数包无法使用 gcc 编译
- constexpr 偏移量,带有指向成员数据的指针
- 指向未由对象地址初始化的对象的指针如何将值分配给类的数据成员
- 将数据成员的指针传递给基类构造函数是否安全?
- 虚拟指针大小因类数据成员而异
- "new"不会将内存分配给作为类的数据成员的指针
- 关于Lambdas,转换以功能指针以及私人数据成员的可见性
- 向上转换指向数据成员及其多态行为的指针
- 指向数据成员转换的 Constexpr 指针
- 关于C++中指向数据成员的指针的一些混淆
- C 细分故障访问类数据成员,而无需明确使用指针