从派生对象获取基对象的地址

发布于 2024-11-16 00:17:14 字数 600 浏览 3 评论 0原文

我的程序中出现了一个非常令人困惑的错误。我认为我可能有同一类的两个不同对象,而我认为我有相同的对象。这很令人困惑,因为我正在处理一个非常大的框架,在该框架中获取指向我需要的对象的指针并不简单。

我的问题是,如果我有一个继承自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

入怼 2024-11-23 00:17:14

从指向派生类的指针转换为指向基类的指针很容易:

Derived * derived_ptr = < some pointer >;
Base * base_ptr = derived_ptr;

如果您想学究气,可以在赋值的右侧使用static_cast

Base * base_ptr = static_cast<Base*>(derived_ptr);

从指针转换为基类到派生类的指针使用dynamic_cast

Derived * derived_ptr = dynamic_cast<Derived*>(base_ptr);

但是,这并不总是有效。您需要启用运行时 typeid,并且基类需要至少有一个虚拟方法。

在执行此操作之前,为什么需要从基指针转到派生指针?这是您可能需要重新考虑您的设计的线索。

Going from a pointer to derived class to a pointer to a base class is easy:

Derived * derived_ptr = < some pointer >;
Base * base_ptr = derived_ptr;

If you want to be pedantic, you can use static_cast on the right hand side of the assignment:

Base * base_ptr = static_cast<Base*>(derived_ptr);

Going from a pointer to a base class to a pointer to a derived class uses dynamic_cast:

Derived * derived_ptr = dynamic_cast<Derived*>(base_ptr);

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.

潇烟暮雨 2024-11-23 00:17:14

只有一个对象,它由一个 Base 和一个 Derived 组成——也就是说,在大多数实现中,Base 被放入内存中紧挨着 Derived。这意味着一般情况下 Base* 与 Derived* 不同,但它们会非常接近。

现在,您可以轻松地从 Derived* 获取 Base*,强制转换是隐式的,但您也可以将其显式显示:

Derived* dptr = ...;
Base* ptr = dptr;

但是,没有什么可以阻止单个派生对象包含多个 基础 对象。通常情况并非如此,但也有可能发生。这意味着您无法比较 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:

Derived* dptr = ...;
Base* ptr = dptr;

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 compare Base pointers and expect to know if you are dealing with the same object, only the same Base subobject.

枯叶蝶 2024-11-23 00:17:14

在单继承的简单情况下,对于大多数编译器:

如果您有一个指向派生类的指针,那么它与指向基类的指针相同。两个指针具有相同的值并指向相同的内存地址。

在内存中,如果创建派生类的实例,它将被布置为基对象的成员,然后是派生对象的成员。基类成员构成派生对象的一部分。

class Base
{
   int b;
};

class Derived : public Base
{
   int d;
};

在内存中,假设 Derived 指针为 0400。那么:

0400 byte 1 of b
0401 byte 2 of b
0402 byte 3 of b
0403 byte 4 of b
0404 byte 1 of d
0405 byte 2 of d
0406 byte 3 of d
0407 byte 4 of d

派生对象由基类成员和派生类自身成员组成,这两个成员的地址都是从 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.

class Base
{
   int b;
};

class Derived : public Base
{
   int d;
};

In memory, say the Derived pointer is 0400. Then:

0400 byte 1 of b
0401 byte 2 of b
0402 byte 3 of b
0403 byte 4 of b
0404 byte 1 of d
0405 byte 2 of d
0406 byte 3 of d
0407 byte 4 of d

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文