从派生对象获取基对象的地址
我的程序中出现了一个非常令人困惑的错误。我认为我可能有同一类的两个不同对象,而我认为我有相同的对象。这很令人困惑,因为我正在处理一个非常大的框架,在该框架中获取指向我需要的对象的指针并不简单。
我的问题是,如果我有一个继承自 Base 的类 Derived,并且我有一个指向 Derived 对象的指针,那么如何从派生对象中获取 Base 对象的地址?我正在使用基类的源代码,并打印出基类中“this”的地址。在我的代码的另一部分中,检索指向 Derived 的指针。我需要能够通过派生对象打印基本对象的地址,以确定是否有指向我需要的特定派生对象的指针。
我可能对 C++ 继承中的地址如何工作有很大的误解。也许它们只是一个对象,而不是链接到派生对象的基础对象?
非常感谢您
编辑: 我想这样做的原因纯粹是为了调试。问题是我正在使用的代码库不包含许多接口或受保护的成员,因此我必须编辑源代码才能访问某些信息。但是,当我使用特定的派生指针调用添加到基类的方法时,我的程序崩溃了。在这种情况下,我需要能够打印基本对象的地址,以便我可以确定这是否是正确的对象,或者我是否收到此错误,因为我实际上有一个指向错误对象的指针。我意识到我可以向派生类添加代码以使其打印其地址,但我只是想知道是否可以在不编辑源代码的情况下获取地址。谢谢
I'm getting a very confusing error in my program. I think I may have two different objects of the same class where I thought I had the same object. It is confusing because I am dealing with a very large framework where it is not simple to obtain a pointer to the object I need.
My question is, if I have a class Derived which in inherits from Base, and I have a pointer to a Derived object, how can I get the address of the Base object from the derived object? I am working with the source code of the Base Class and am printing out the address of "this" in Base. In another part of my code a retrieve a pointer to a Derived. I need to be able to print the address of the Base object via my Derived object to determine whether I have a pointer to the particular Derived object I need.
I may have a great misunderstanding of how the addresses work in C++ in inheritance. Perhaps their is only one object, not a base object linked to a derived object?
Thank you very much
Edit:
The reason I want to do this is purely for debugging. The problem is that the code base I'm using does not contain many interfaces or protected members, so I am having to edit the source code to access a certain piece of information. However, my program crashes when I call a method I added to the Base class using a specific Derived pointer. I need to be able to print the address of the base object in this case so that I can determine whether this is the correct object or whether I am getting this error because I actually have a pointer to the wrong object. I realize I can add code to the derived class to cause it to print its address, but I was just wondering if it was possible to get the address without editing the source code any more. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从指向派生类的指针转换为指向基类的指针很容易:
如果您想学究气,可以在赋值的右侧使用
static_cast
:从指针转换为基类到派生类的指针使用
dynamic_cast
:但是,这并不总是有效。您需要启用运行时 typeid,并且基类需要至少有一个虚拟方法。
在执行此操作之前,为什么需要从基指针转到派生指针?这是您可能需要重新考虑您的设计的线索。
Going from a pointer to derived class to a pointer to a base class is easy:
If you want to be pedantic, you can use
static_cast
on the right hand side of the assignment:Going from a pointer to a base class to a pointer to a derived class uses
dynamic_cast
:However, this won't always work. You need to have run-time typeid enabled and the base class needs to have at least one virtual method.
Before you go doing this, why do you need to go from a base pointer to a derived pointer? That is a clue that you may need to rethink your design.
只有一个对象,它由一个 Base 和一个 Derived 组成——也就是说,在大多数实现中,Base 被放入内存中紧挨着 Derived。这意味着一般情况下 Base* 与 Derived* 不同,但它们会非常接近。
现在,您可以轻松地从 Derived* 获取 Base*,强制转换是隐式的,但您也可以将其显式显示:
但是,没有什么可以阻止单个派生对象包含多个
基础
对象。通常情况并非如此,但也有可能发生。这意味着您无法比较Base
指针并期望知道您是否正在处理相同的对象,只是相同的Base
子对象。There is only one object, it is composed of a Base and a Derived- that is, the Base is put into memory right next to the Derived, in most implementations. That means that the Base* is not the same as Derived* in the general case, but they will be very close.
Now, you can trivially obtain the Base* from the Derived*, the cast is implicit, but you can also make it explicit:
However, there is nothing stopping a single derived object from containing multiple
Base
objects. Usually this is not the case, but it can happen. That means that you cannot compareBase
pointers and expect to know if you are dealing with the same object, only the sameBase
subobject.在单继承的简单情况下,对于大多数编译器:
如果您有一个指向派生类的指针,那么它与指向基类的指针相同。两个指针具有相同的值并指向相同的内存地址。
在内存中,如果创建派生类的实例,它将被布置为基对象的成员,然后是派生对象的成员。基类成员构成派生对象的一部分。
在内存中,假设 Derived 指针为 0400。那么:
派生对象由基类成员和派生类自身成员组成,这两个成员的地址都是从 0400 开始的。
恰好,在 0400 处,基类对象部分派生位于。因此,基地址和派生地址具有相同的地址。
In the simple case of single inheritance, with most compilers:
If you've got a pointer to the derived class, then that's the same as the pointer to the base class. Both pointers have the same value and point to the same memory address.
In memory, if you create an instance of a derived class, it will be laid out as the members of the base object, followed by the members of the derived object. The base class members form part of the derived object.
In memory, say the Derived pointer is 0400. Then:
The derived object consists of the base members and derived's own members, and the address of both of these starts at 0400.
It just so happens, that at 0400, the base object part of derived is located. So, base and derived have the same address.