简体   繁体   English

在C ++中,派生类指针应指向基类指针吗?

[英]In C++ should derived class pointer point to base class pointer?

I've very rare error and to understand the reason I need to know some facts of C++. 我有一个非常罕见的错误,并且要了解我需要了解C ++某些事实的原因。 Let me explain the problem: 让我解释一下这个问题:

There is such structure: 有这样的结构:

struct Base;                 // not polimorfic
class Derived : public Base; // has pure virtual
class ImplDerived : public Derived    // implements pure virtuals

some_function(Base* base);
int main() {
    ImplDerived impd;
    some_function(&impd);
}

Now, what's happening is this: 现在,这是怎么回事:

  • Running on desktop linux environment: 桌面linux环境上运行:

    No error. 没错

    Inside some_function , base pointer points to the same address of &impd . some_function内部, 基本指针指向&impd的相同地址。 In other words, in memory, ImplDerived object starts with Base object. 换句话说,在内存中,ImplDerived对象从Base对象开始。 ie, when I'm debugging, I see this: 即,当我调试时,我看到以下内容:

     ImplDerived : 0xab00 -> Derived : 0xab00 -> Base : 0xab00 
  • Running on embedded linux in embedded device (compiled with its compiler): 在嵌入式设备(与其编译器编译)中的嵌入式Linux上运行:

    Error. 错误。

    Inside some_function , base pointer points again to &impd . some_function内部, 基本指针再次指向&impd

    However, this time it is not the right place where base is. 但是,这一次不是合适的基地 Because now 因为现在
    base starts at &impd +4 bytes. 基本&impd +4字节开始。

    When I'm debugging I see this: 当我调试时,我看到以下内容:

     ImplDerived : 0xab00 -> Derived : 0xab00 -> Base : 0xab04 

Questions: 问题:

  • AFAIK, the offset between base and derived pointer is permittible, right ? AFAIK,基本指针与派生指针之间的偏移量是允许的, 吗?

  • If it is, then should not the implicit casting when passing the pointer to the some_function find where the base is? 如果是,那么在将指针传递给some_function时,隐式转换是否不应该找到base在哪里?

  • If it should, then would it be some error in compilation? 如果可以,那么编译时会出错吗?

Thanks. 谢谢。

Compilers: 编译器:

  • Desktop: gcc 4.9.2-10 Debian 64 bits 桌面:gcc 4.9.2-10 Debian 64位
  • Embedded: gcc 4.8.0 嵌入式:gcc 4.8.0

EDIT (RESULT): 编辑(结果):

After all tests, we have decided that this is a compiler error. 经过所有测试,我们认为这是编译器错误。 Casting just does not work correctly, i) for non-polymorphic base, ii) for multiple inheritance. 强制转换无法正常工作,i)非多态基础,ii)多重继承。

No, pointers to base and derived classes do not have to match. 不,指向基类和派生类的指针不必匹配。 Especially this happens when multiple inheritance is used. 特别是在使用多重继承时,会发生这种情况。 In your case the reason seems different. 在您的情况下,原因似乎有所不同。 Because the base class is not polymorphic, it does not have a virtual table pointer as its hidden member. 因为基类不是多态的,所以它没有虚拟表指针作为其隐藏成员。 However, the polymorphic derived classes start to have virtual table pointer. 但是,多态派生类开始具有虚拟表指针。 I guess you are compiling in 32-bit, so that's where 4 bytes offset arises: that pointer is 32-bit. 我猜您正在以32位进行编译,所以这就是4字节偏移量出现的地方:该指针是32位的。

dynamic_cast should solve your problem. dynamic_cast应该可以解决您的问题。

AFAIK, the offset between base and derived pointer is permittible, right? AFAIK,基本指针与派生指针之间的偏移量是允许的,对吗?

It is permitted, yes. 可以,可以。 In fact, it's mandatory if the derived class has multiple (non-empty) base classes, since all of the base class sub objects cannot share the same address. 实际上,如果派生类具有多个(非空)基类,则是强制性的,因为所有基类的子对象不能共享同一地址。 But even a sole base class sub object might not share the derived object's address, as you have observed. 但是,正如您所观察到的,即使是唯一的基类子对象也可能不会共享派生对象的地址。

If it is, then should not the implicit casting when passing the pointer to the some_function find where the base is? 如果是,那么在将指针传递给some_function时,隐式转换是否不应该找到基数在哪里?

Yes. 是。 When the derived pointer is implicitly converted to the base pointer, it should be pointing to the base sub object. 当派生指针隐式转换为基本指针时,它应指向基本子对象。

If it should, then would it be some error in compilation? 如果可以,那么编译时会出错吗?

Maybe, the example code should be just fine, but it's incomplete. 也许示例代码应该还不错,但是还不完整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM