English 中文(简体)
如何使用重新解释__ 投放到C++的衍生班子
原标题:how to use reinterpret_cast to cast to a derived class pointer in c++

我的试验例子如下:

struct base {
    virtual ~base(){}
    int x;
};

struct derived: public virtual base {
    base * clone() {
        return new derived;
    }
    derived(): s("a") {}
    std::string s;
};

int main () {
    derived d;
    base * b = d.clone();
    derived * t = reinterpret_cast<derived*>(b);
    std::cout << t->s << std::endl;
    return 0;
}

由于“b”是衍生产品类别的一个点,因此,重新解释——播像应当只是工作。 我想知道它为什么坠毁。 与此同时,如果我用动态节目取代重新解释,那么它就能够发挥作用。

最佳回答

即便在<条码>b>上<>>>>>>>>> ,您也必须使用<条码>动力学_cast。 这就是<代码>动力学-cast用于动态地将一个基级的点子转换成一个衍生级。

reinterpret_cast takes the raw pointer and considers it as being of the derived type. However, because of the virtual inheritance, a slight adjustment must be done to the pointer to point to the correct method dispatch table, and that s precisely what dynamic_cast will do.

问题回答

Don t reinterpret_cast, 这将与你一样造成多重或虚拟遗产的困扰。 缩略语 这里的工作如何?

To know why, search for implementations of virtual inheritance. A common one is to store a pointer to the base class within the object, so the virtual base does not share the same address than its derived classes. There is a similar case when multiple inheritance is used.

简言之,reinterpret_cast 做的只是把点推到ts和背面(如果 in体内有足够大的尺寸以包含点子)。

如本文中的其他答复所示,你不能以这种方式使用<代码>reinterpret_cast,因为点名至数值> 点名至derived的实际数值有所不同。 有效点员在运行时间被抽出,因此,您必须使用<条码>动力学-cast<>code>。

这里的真正问题是:在设计时间,我知道如何从<条码>底线/代码>点点对<条码>代号>。 如何避免(<代码>动力学-cast>)的时效?

坦率地说,我在这里看不出一个真正好的选择,但possible的备选办法是把点人储存在根类中一个固定点,如:

struct base {
  void* const self;
  virtual ~base() {}
  protected:
    base(void* self) : self(self) {}
};
struct derived : public virtual base {
  derived() : base(this) {}
}

这种做法非常危险,因为它牺牲了业绩的类型安全(如果你真心 l,你就会获得<>光,从中抽出。 但是,您将可在<代码>reinterpret_cast>上将( Pointer( per member of category Without* 输入 pointer。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签