从派生对象获取基础对象的地址 [英] Get address of base object from derived object

查看:107
本文介绍了从派生对象获取基础对象的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中出现一个非常混乱的错误.我认为我可能在同一类中有两个不同的对象,而我以为我有相同的对象.这令人困惑,因为我正在处理一个非常大的框架,在该框架中很难获得指向所需对象的指针.

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.

我的问题是,如果我有一个继承自Base的Derived类,并且有一个指向Derived对象的指针,我如何从该派生对象中获取Base对象的地址?我正在使用基类的源代码,并在基类中打印出"this"的地址.在我的代码的另一部分中,检索指向派生对象的指针.我需要能够通过Derived对象打印Base对象的地址,以确定我是否有指向所需的特定Derived对象的指针.

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.

我可能会对地址在继承中的C ++中的工作方式有很大的误解.也许它们只是一个对象,而不是链接到派生对象的基础对象?

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?

非常感谢您

我要这样做的原因纯粹是为了调试.问题是我正在使用的代码库不包含许多接口或受保护的成员,因此我必须编辑源代码才能访问某些信息.但是,当我调用使用特定的Derived指针添加到Base类中的方法时,程序崩溃.在这种情况下,我需要能够打印基础对象的地址,以便可以确定这是正确的对象还是因为我实际上有一个指向错误对象的指针而出现此错误.我意识到我可以将代码添加到派生类中,以使其打印其地址,但是我只是想知道是否有可能在不编辑源代码的情况下获得该地址.谢谢

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

推荐答案

从指向派生类的指针到指向基类的指针很容易:

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;

如果要上学,可以在作业的右侧使用static_cast:

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);

从指向基类的指针到指向派生类的指针使用dynamic_cast:

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);

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

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.

这篇关于从派生对象获取基础对象的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆