C++多继承
问题描述:
可能显示的文件:
C++ pointer multi-inheritance fun.
more c++ multiple inheritance funC++多继承
这是从与参考计数指针基类处理和线程乐趣出现的一个问题。
考虑:
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 = CtoB(pc);
A* pa = CtoA(pc);
assert(pc == AtoC(pa));
assert(pc == BtoC(pb));
我怎样写CtoB和CTOA得到的B &℃的部分?
如何我写ATOC和BtoC的找回原来的C?
谢谢!
为何选票关闭?
我以前的两个问题问什么是有效的(答案是“否”);这个问题问“什么是做指针转换的有效方法”。
答
你不需要任何功能,除非你从A点或B多次(不包括虚拟继承)派生而来,因为仅C从A和B派生一次,你只需要使用:
A *pbb = pc;
B *pba = pc;
AtoC和BtoC只能通过以下方式安全使用:
C *c = dynamic_cast<C*>(a_or_b_pointer);
您是否真的需要第三个相同主题的问题? http://stackoverflow.com/questions/2158512/more-c-multiple-inheritance-fun和http://stackoverflow.com/questions/2157104/c-pointer-multi-inheritance-fun – Trent 2010-01-28 22:56:29