更多c ++多继承的乐趣 [英] more c++ multiple inheritance fun

查看:167
本文介绍了更多c ++多继承的乐趣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C ++指针多继承有趣

on: http://stackoverflow.com/questions/2157104/c-pointer-multi-inheritance- fun

我正在写一些涉及从基本的计数指针类继承的代码;和一些复杂的C ++弹出。我已将其减少如下:

I'm writing some code involving inheritance from a basic ref-counting pointer class; and some intricacies of C++ popped up. I've reduced it as follows:

假设我有:

class A{int x, y;};
class B{int xx, yy;};
class C: public A, public B {int z;};

C c;
C* pc = &c;
B* pb = &c;
A* pa = &c;

// does pa point to a valid A object?
// does pb point to a valid B object?

// does pa == pb ?

此外,还有:

// pc == (C*) pa ?
// pc == (C*) pb ?

谢谢!

=表示点数相同。

推荐答案




  • 没有


  • li>

    • Yes
    • Yes
    • No
    • Yes
    • No*
    • 但是你需要知道C ++如何组织内存。类的布局,CClass如下:

      But you need to know how C++ organises memory. The layout of a class, CClass, is as follows:

      offset
      0                              First base class in list of base classes
      sizeof first base class        Next base class
      sizeof first N-1 base classes  Nth base class
      sizeof all base classes        CClass
      

      好吧,如果有一个继承树,它会有点复杂,但你应该得到基本的想法。假设一个int是四个字节,则C类的布局如下:

      OK, it's a bit more complex than that if there is an inheritance tree, but you should get the basic idea. Assuming an int is four bytes then class C is laid out like:

      offset
      0             A::x
      4             A::y
      8             B::xx
      12            B::yy
      16            C:z
      

      但是类型C的一个对象是上面所有的,所以指向一个C的指针指向偏移0.然而,C ++允许隐式向下转换,所以一个指向C可以转换为指向A的指针或指向B的指针。但是,如果你看上面的,一个指向B的指针在偏移量8而不是0(指向C的指针),而指向A的指针因此,将指针指向一个C指针指向一个B,指针值将增加8。这是因为B的方法假设this指针指向B的第一个成员(B :: xx),但是如果重新解释为指向B的指针(即值相同)的C指针将是指针到B的实际位置之前的八个字节的地址,以便所有的B的方法将在这种情况下使用A的成员。

      But the an object of type C is all of the above, so a pointer to a C is pointing to offset 0. However, C++ allows implicit downcasting, so a pointer to a C can be converted to a pointer to an A or a pointer to a B. But if you look at the above, a pointer to a B is at offset 8 rather than 0 (a pointer to a C) and that a pointer to an A is at offset 0. So, casting a pointer to a C to a pointer to a B adds 8 to the pointer value. This is because the methods of B assume the 'this' pointer points to B's first member (B::xx), but a pointer to a C if reinterpreted as a pointer to a B (i.e. the value is the same) would be pointer to an address eight bytes before where B actually is, so that all of B's methods would be using, in this instance, A's members.

      向上转换(最后两个转换)是不同的水壶的鱼。从指针到B到指向C的指针真的很难,因为你不知道指向B的指针是指向B的实例还是指向C的实例。这是RTTI和动态转换的地方。使用RTTI(运行时类型信息)启用,指向B的指针包含描述B实际是什么的附加信息 - 一个简单的B或B作为C的一部分。额外的成本,执行时间和内存使用。

      Upcasting (the final two conversions) is different kettle of fish. Going from a pointer to a B to a pointer to a C is really hard because you don't know if the pointer to a B is just pointing to an instance of B or at an instance of C plus eight. This is where RTTI and the dynamic cast comes in. With RTTI (Run Time Type Information) enabled, the pointer to B contains additional information that describes what B really is - a simple B or a B as part of a C. This does have additional cost, both execution time and memory usage.

      最后,这突出了C风格转换的模糊性。你真的应该使用C ++风格的cast(static_cast<>,等),因为这澄清了转换的方式。

      Finally, this does highlight the ambiguity of the C style cast. You really should use the C++ style casts (static_cast <>, etc) as this clarifies the way the conversion should be done.

      * 这也可能是一个是,我想这取决于编译器,如果RTTI是开或关。你需要了解标准的细节和编译器实现来肯定地说。

      *This could also be a yes, I guess it depends on the compiler and if RTTI is on or off. You would need to get into the details of the standard and the compiler implementation to say for sure.

      这篇关于更多c ++多继承的乐趣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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