c++ - 指针是否允许在单继承中更改值?

标签 c++ pointers casting language-lawyer multiple-inheritance

我知道通过多重继承,指针的值是可以改变的。但是单继承也是这样吗?还是与 POD 类型有关?

你可能知道经典的例子:

#include <iostream>
using std::cout;
struct Base1 { virtual void f1() {} };
struct Base2 { virtual void f2() {} };
struct Derived : public Base1, public Base2 { virtual void g() {} };
int main() {
    Derived d{};
    auto *pd = &d;
    auto pb1 = (Base1*)pd;
    auto pb2 = (Base2*)pd;
    cout << pd << "\n"; // say, 0x1000
    cout << pb1 << "\n"; // say, 0x1000
    cout << pb2 << "\n"; // say, 0x1008 !!!
}

到目前为止一切顺利,这是很好的旧编译器实践。对象的布局方式是 Base2 的“root”与 Derived 有一个偏移量,可以在查看实际指针值时打印出来。当避免执行 void*reinterpret_cast 时,这不是问题。

据我所知,在实践中这只会发生在多重继承中。

但我的问题是:标准对“转换期间指针更改值”有何规定?这是否发生在多重继承中,或者也可能发生在单继承或转换 POD 中?

最佳答案

如果类型是标准布局,则在任何继承级别指向包含结构的指针都等于指向第一个元素的指针。通过传递性,指向所有继承级别的指针也是相同的——并且标准继续进行,并且在不存在非静态数据成员的奇怪情况下也做出了这种保证。

(请注意,要成为标准布局,一个类型只能在继承图中的一个级别引入数据成员——更多的子类可以添加功能并保持标准布局,但不能添加更多数据)。

如果类型不是标准布局,则不做任何保证,您必须使用 static_cast,而不是 reinterpret_cast(也不是 reinterpret_cast 等价物,例如多个 static_cast 和 void* 作为中间值)以正确包含任何偏移量。

当然,所有 POD 类型都是标准布局,PODness 既需要标准布局又需要琐碎。

我正在解释的确切规则可以在第 9.2 节中找到:

If a standard-layout class object has any non-static data members, its address is the same as the address of its first non-static data member. Otherwise, its address is the same as the address of its first base class subobject (if any).

[ Note: There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment. — end note ]

[ Note: The object and its first subobject are pointer-interconvertible. — end note ]

第 10 节中明确指出对于非标准布局的类型可能存在奇数布局的规则:

The order in which the base class subobjects are allocated in the most derived object is unspecified.

关于c++ - 指针是否允许在单继承中更改值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42447811/

相关文章:

c# - 为什么在 .NET 中转换比反射快?

c++ - 在 OpenGL 中缩放投影矩阵时出现意外行为

C++模板模板(双模板?)

c - C 中的指针错误?

c++ - 在浮点值 C++ 中存储整数除法

java - 将子类型的 HashMap 转换为父类(super class)型的 HashMap

c++ - 为什么使用 cv::line 函数绘制的不同线条具有不同的透明度 (alpha) 级别?

c++ - Qt在菜单项单击时显示模式对话框(.ui)

c - 从 stdin 动态分配行?

c - 从数组中获取 C 中某些值的内存地址