美文网首页
C++多重继承的成员指针问题

C++多重继承的成员指针问题

作者: 一剑卿心 | 来源:发表于2018-03-16 13:00 被阅读33次

    1.cocos2dx开发中遇到一个多继承的问题:ResourceDownloader定义如下:

    class ResourceDownloader : public GCloud::IGcloudPufferCallBack, public cocos2d::Ref
    

    需要在该类中每帧执行Update函数,
    Director::sharedDirector()->getScheduler()->schedule(schedule_selector(ResourceDownloader::Update), this, 0, false);

    链接时出现一个link error指向刚刚这行代码,具体错误信息如下:
    error C2220: warning treated as error - no 'object' file generated.
    warning C4407:cast between different pointer to member representations, compiler may generate incorrect code.

    多继承Error2.png

    2.解决方案:交换基类的位置——使得Ref在前

    class ResourceDownloader :public cocos2d::Ref, public GCloud::IGcloudPufferCallBack
    

    3.原因分析

    截图于网址:https://msdn.microsoft.com/en-us/library/1s6193tt.aspx

    C4407 can occur if you cast between a multiple inheritance pointer-to-member to a single inheritance pointer-to-member. Sometimes this can work, but sometimes it can’t because the single inheritance pointer-to-member representation doesn’t hold sufficient information. Compiling with the /vmm might help (for more information, see /vmm, /vms, /vmv (General Purpose Representation)). You can also try rearranging your base classes; the compiler is detecting a loss of information in the conversion because a base class is at a non-zero offset from the derived.

    如果在多重继承的指向成员的指针与单一继承的指向成员的指针之间进行强制转换,则可能发生C4407。 有时候这种强制转换是可以的,但有时却不行;因为单一继承的指向成员的指针没有办法承载足够多的信息。 使用/vmm命令进行编译可能得到有用信息(有关更多信息,请参阅/ vmm、/ vms、/ vmv)。 或者你可以尝试重新排序你的基类; 因为基类和派生类之间如果存在偏移值,编译器在处理强制转换时就可能检测到信息的丢失(或者说认为存在信息的丢失),进而发出警告。

    相关文章

      网友评论

          本文标题:C++多重继承的成员指针问题

          本文链接:https://www.haomeiwen.com/subject/idilqftx.html