C 多重继承 - 编译器修改我的指针
c++ Multiple Inheritance - Compiler modifying my pointers
如果我运行以下代码,我会打印不同的地址。为什么?
class Base1 {
int x;
};
class Base2 {
int y;
};
class Derived : public Base1, public Base2 {
};
union U {
Base2* b;
Derived* d;
U(Base2* b2) : b(b) {}
};
int main()
{
Derived* d = new Derived;
cout << d << "n";
cout << U(d).d << "n";
return 0;
}
更有趣的是,如果您反复进出联盟,地址会继续增加4个,例如
int main()
{
Derived* d = new Derived;
cout << d << "n";
d = U(d).d;
cout << d << "n";
d = U(d).d;
cout << d << "n";
return 0;
}
如果工会被这样修改,那么问题就会消失
union U {
void* v;
Base2* b;
Derived* d;
U(void* v) : v(v) {}
};
另外,如果将任何一个基类都是空的,那么问题就会消失。这是编译器错误吗?我希望它让我的指针独自一人。
如果运行以下代码,我会收到不同的地址。为什么?
因为Derived
对象的Base2
子对象不是Derived
对象的开始。因此地址不同。当编译器执行从Derived*
到Base2*
的隐式铸件时,它需要调整地址。
给定Base1
和Base2
类的定义,Derived
类的两个子对象都不可能在Derived
对象的起始地址上 - 两个子对象的该地址都没有空间。
说您有此代码:
Derived* d = new Derived;
Base1* pb1 = d;
Base2* pb2 = d;
pb1
和pb2
如何指向相同的地址?pb1
必须指向Base1::x
项目,pb2
必须指向Base2::y
项目(这些项目必须不同)。
更有趣的是,如果您反复进出联盟,地址会继续增加4
因为您正在撰写b
成员之后从工会的d
成员阅读,这是不确定的行为(从本质上讲,您在Base2*
上执行了类似reinterpret_cast<Derived*>()
的内容)。
我希望它让我的指针独自一人。
如果您想要Base2*
指针。多重继承使事情变得更加复杂 - 这就是为什么许多人建议避免使用它,除非绝对必要。
联合构造函数从不初始化成员d
联合构造函数有一个错误,其中它不是用参数B2初始化成员B,而是用自身初始化b
// b(b) should probably be b(b2)
U(Base2* b2) : b(b) {}
当您的第一个主要函数示例尝试构造u的实例并打印成员D时,它实际上是在打印未定义的值,因为成员d尚未初始化,并且不能保证可访问。
// U(d) doesn't construct member d, so .d returns an undefined value
cout << U(d).d << "n";
关于您的第二个主要功能示例
// d is set to a newly constructed instance of Derived
Derived* d = new Derived;
// current address of d is printed
cout << d << "n";
// a new instance of U is constructed. The address of member d will be in close
// proximity to the newly initialized U instance, and is what will be printed
d = U(d).d;
cout << d << "n";
// yet another new instance of U is constructed, and again, the address of member
// d will be in close proximity to the newly initialized U instance, and is
//what will be printed
d = U(d).d;
cout << d << "n";
相关文章:
- C++为什么我的指针选择排序中存在分段错误?
- 指针相关的UE4崩溃.我的指针哪里错了?
- 为什么我的指针数组分配不起作用
- 为什么我的指针不是结构或联合?
- 如果我不使用 vector.reserve(),为什么我的指针会失效?
- 为什么我的指针会导致段错误
- 我的指针代码 c++ 中的分段错误(核心转储)
- 如何在我的指针和阵列功能中修复未解决的外部错误-LNK2019和LNK1120
- 在C++和Windows中如何处理我的指针
- C 多重继承 - 编译器修改我的指针
- 为什么我的指针在 main 中取消引用后具有不同的值,而不是在我自己的自定义函数中取消引用它时?
- 无法让我的指针发送对数组的引用
- 为什么我的指针出现分段错误
- 保证我的指针内存容器稍后存在
- C++ LP64 中通过引用更改我的指针值传递.我应该看什么
- 什么负责删除我的指针
- 我的指针有什么问题(open_stackdumpfile错误)?
- 为什么我的指针输出的是字符串,而不是C++中的内存地址
- 为什么我的指针会自动改变
- 试图创建一个程序来读取用户输入,然后将数组分解为单独的单词,我的指针都有效吗