multiple-inheritance

Using C++, how do I correctly inherit from the same base class twice?

不打扰是莪最后的温柔 提交于 2019-11-30 05:15:46
This is our ideal inheritance hierarchy: class Foobar; class FoobarClient : Foobar; class FoobarServer : Foobar; class WindowsFoobar : Foobar; class UnixFoobar : Foobar; class WindowsFoobarClient : WindowsFoobar, FoobarClient; class WindowsFoobarServer : WindowsFoobar, FoobarServer; class UnixFoobarClient : UnixFoobar, FoobarClient; class UnixFoobarServer : UnixFoobar, FoobarServer; This is because the our inheritance hierarchy would try to inherit from Foobar twice, and as such, the compiler would complain of ambiguous references on any members of Foobar . Allow me to explain why I want such

How is C++'s multiple inheritance implemented?

故事扮演 提交于 2019-11-30 04:44:13
Single inheritance is easy to implement. For example, in C, the inheritance can be simulated as: struct Base { int a; } struct Descendant { Base parent; int b; } But with multiple inheritance, the compiler has to arrange multiple parents inside newly constructed class. How is it done? The problem I see arising is: should the parents be arranged in AB or BA, or maybe even other way? And then, if I do a cast: SecondBase * base = (SecondBase *) &object_with_base1_and_base2_parents; The compiler must consider whether to alter or not the original pointer. Similar tricky things are required with

C++ Multiple Inheritance Memory Layout with “Empty classes”

时光总嘲笑我的痴心妄想 提交于 2019-11-30 03:51:15
问题 I know the memory layout of multiple inheritance is not defined, so I should not rely on it. However, can I rely on it in a special case. That is, a class has only one "real" super class. All others are "empty classes", i.e., classes that neither have fields nor virtual methods (i.e. they only have non-virtual methods). In this case, these additional classes should not add anything to the memory layout of the class. (More concisely, in the C++11 wording, the class has standard-layout ) Can I

Why are classes being ordered this way in the MRO?

醉酒当歌 提交于 2019-11-30 03:42:48
问题 I have a problem with the Python MRO For this code: class F: pass class G: pass class H: pass class E(G,H): pass class D(E,F): pass class C(E,G): pass class B(C,H): pass class A(D,B,E): pass print(A.__mro__) I get this output: (<class '__main__.A'>, <class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.E'>, <class '__main__.G'>, <class '__main__.H'>, <class '__main__.F'>, <class 'object'>) Why do I get <class '__main__.C'> before <class '__main__.E'> ? I thought

Python Call Parent Method Multiple Inheritance

只谈情不闲聊 提交于 2019-11-30 01:52:06
问题 So, i have a situation like this. class A(object): def foo(self, call_from): print "foo from A, call from %s" % call_from class B(object): def foo(self, call_from): print "foo from B, call from %s" % call_from class C(object): def foo(self, call_from): print "foo from C, call from %s" % call_from class D(A, B, C): def foo(self): print "foo from D" super(D, self).foo("D") d = D() d.foo() The result of the code is foo from D foo from A, call from D I want to call all parent method, in this case

How does multiple inheritance work with the super() and different __init__() arguments?

时间秒杀一切 提交于 2019-11-29 21:31:27
I'm just diving into some more advanced python subjects (well, advanced to me at least). I am now reading about multiple inheritance and how you can use super(). I more or less understand the way the super function is used, but (1) What's wrong with just doing it like this ?: class First(object): def __init__(self): print "first" class Second(object): def __init__(self): print "second" class Third(First, Second): def __init__(self): First.__init__(self) Second.__init__(self) print "that's it" On to super(), Andrew Kuchlings paper on Python Warts says: usage of super() will also be correct when

How did C#'s lack of multiple inheritance lead to the need for interfaces?

好久不见. 提交于 2019-11-29 21:15:57
In The C# Programming Language Krzysztof Cwalina states in an annotation: we explicitly decided not to add support for multiple inheritance [...] the lack of multiple inheritance forced us to add the concept of interfaces, which in turn are responsible for problems with the evolution of the framework, deeper inheritance hierarchies, and many other problems. Interfaces are a core concept to OO programming languages. I don't follow the meaning of "forced us to add the concept of interfaces" Does Krzysztof mean that certain design decisions had to be made regarding the use of interfaces where

Multiple inheritance in python3 with different signatures

左心房为你撑大大i 提交于 2019-11-29 19:58:05
I have three classes: A , B and C . C inherits from A and B (in this order). The constructor signatures of A and B are different. How can I call the __init__ methods of both parent classes? My endeavour in code: class A(object): def __init__(self, a, b): super(A, self).__init__() print('Init {} with arguments {}'.format(self.__class__.__name__, (a, b))) class B(object): def __init__(self, q): super(B, self).__init__() print('Init {} with arguments {}'.format(self.__class__.__name__, (q))) class C(A, B): def __init__(self): super(A, self).__init__(1, 2) super(B, self).__init__(3) c = C() yields

Using Qt signals and slots with multiple inheritance

六眼飞鱼酱① 提交于 2019-11-29 18:42:34
问题 I have a class ( MyClass ) that inherits most of its functionality from a Qt built-in object ( QGraphicsTextItem ). QGraphicsTextItem inherits indirectly from QObject . MyClass also implements an interface, MyInterface . class MyClass : public QGraphicsTextItem, public MyInterface I need to be able to use connect and disconnect on MyInterface* . But it appears that connect and disconnect only work on QObject* instances. Since Qt does not support multiple inheritance from QObject-derived

Double inheritance of enable_shared_from_this

蹲街弑〆低调 提交于 2019-11-29 18:19:43
问题 I have an object (Z) which derives from two other objects (A and B). A and B both derive from enable_shared_from_this<> , respectively enable_shared_from_this<A> and enable_shared_from_this<B> . Of course I call shared_from_this() on Z. And of course the compiler reports this as ambiguous. My questions are : is it safe to inherit twice from enable_shared_from_this<> or will it create two separated reference counts (bad !) If not safe, how do I solve this ? Note : I've found this other