multiple-inheritance

Virtual tables and memory layout in multiple virtual inheritance

限于喜欢 提交于 2019-11-26 08:25:09
问题 Consider following hierarchy: struct A { int a; A() { f(0); } A(int i) { f(i); } virtual void f(int i) { cout << i; } }; struct B1 : virtual A { int b1; B1(int i) : A(i) { f(i); } virtual void f(int i) { cout << i+10; } }; struct B2 : virtual A { int b2; B2(int i) : A(i) { f(i); } virtual void f(int i) { cout << i+20; } }; struct C : B1, virtual B2 { int c; C() : B1(6),B2(3),A(1){} virtual void f(int i) { cout << i+30; } }; What\'s the exact memory layout of C instance? How many vptrs it

How does Python&#39;s “super” do the right thing?

佐手、 提交于 2019-11-26 07:59:08
问题 I\'m running Python 2.5, so this question may not apply to Python 3. When you make a diamond class hierarchy using multiple inheritance and create an object of the derived-most class, Python does the Right Thing (TM). It calls the constructor for the derived-most class, then its parent classes as listed from left to right, then the grandparent. I\'m familiar with Python\'s MRO; that\'s not my question. I\'m curious how the object returned from super actually manages to communicate to calls of

Multiple Inheritance in java

女生的网名这么多〃 提交于 2019-11-26 07:45:08
问题 Java is not allowing inheritance from multiple classes (still it allows inheritance from multiple interfaces.), I know it is very much inline with classic diamond problem. But my questions is why java is not allowing multiple inheritance like C++ when there is no ambiguity (and hence no chances of diamond problem) while inheriting from multiple base class ? 回答1: It was a design decision of Java. You'll never get it, so don't worry too much about it. Although MI might help you make Mixins,

What is the exact problem with multiple inheritance?

百般思念 提交于 2019-11-26 06:43:30
I can see people asking all the time whether multiple inheritance should be included into the next version of C# or Java. C++ folks, who are fortunate enough to have this ability, say that this is like giving someone a rope to eventually hang themselves. What’s the matter with multiple inheritance? Are there any concrete samples? benjismith The most obvious problem is with function overriding. Let's say have two classes A and B , both of which define a method doSomething . Now you define a third class C , which inherits from both A and B , but you don't override the doSomething method. When

Can an interface extend multiple interfaces in Java?

落爺英雄遲暮 提交于 2019-11-26 06:16:51
问题 Can an interface extend multiple interfaces in Java? This code appears valid in my IDE and it does compile: interface Foo extends Runnable, Set, Comparator<String> { } but I had heard that multiple inheritance was not allowed in Java. Why does there appear to be an exception for interfaces? 回答1: Yes, you can do it. An interface can extend multiple interfaces, as shown here: interface Maininterface extends inter1, inter2, inter3 { // methods } A single class can also implement multiple

Why exactly do I need an explicit upcast when implementing QueryInterface() in an object with multiple interfaces()

99封情书 提交于 2019-11-26 05:37:31
问题 Assume I have a class implementing two or more COM interfaces: class CMyClass : public IInterface1, public IInterface2 { }; Almost every document I saw suggests that when I implement QueryInterface() for IUnknown I explicitly upcast this pointer to one of the interfaces: if( iid == __uuidof( IUnknown ) ) { *ppv = static_cast<IInterface1>( this ); //call Addref(), return S_OK } The question is why can\'t I just copy this ? if( iid == __uuidof( IUnknown ) ) { *ppv = this; //call Addref(),

How to avoid infinite recursion with super()?

二次信任 提交于 2019-11-26 05:34:08
问题 I have code like this: class A(object): def __init__(self): self.a = 1 class B(A): def __init__(self): self.b = 2 super(self.__class__, self).__init__() class C(B): def __init__(self): self.c = 3 super(self.__class__, self).__init__() Instantiating B works as expected but instantiating C recursed infinitely and causes a stack overflow. How can I solve this? 回答1: When instantiating C calls B.__init__ , self.__class__ will still be C, so the super() call brings it back to B. When calling super(

Does C# support multiple inheritance?

不想你离开。 提交于 2019-11-26 05:32:52
问题 A colleague and I are having a bit of an argument over multiple inheritance. I\'m saying it\'s not supported and he\'s saying it is. So, I thought that I\'d ask the brainy bunch on the net. 回答1: Nope, use interfaces instead! ^.^ 回答2: Sorry, you cannot inherit from multiple classes . You may use interfaces or a combination of one class and interface(s) , where interface(s) should be followed by class name in the signature. interface A { } interface B { } class Base { } class AnotherClass { }

How can I avoid the Diamond of Death when using multiple inheritance?

ぃ、小莉子 提交于 2019-11-26 05:27:40
问题 http://en.wikipedia.org/wiki/Diamond_problem I know what it means, but what steps can I take to avoid it? 回答1: A practical example: class A {}; class B : public A {}; class C : public A {}; class D : public B, public C {}; Notice how class D inherits from both B & C. But both B & C inherit from A. That will result in 2 copies of the class A being included in the vtable. To solve this, we need virtual inheritance. It's class A that needs to be virtually inherited. So, this will fix the issue:

How to make a Java class that implements one interface with two generic types?

荒凉一梦 提交于 2019-11-26 04:37:52
问题 I have a generic interface public interface Consumer<E> { public void consume(E e); } I have a class that consumes two types of objects, so I would like to do something like: public class TwoTypesConsumer implements Consumer<Tomato>, Consumer<Apple> { public void consume(Tomato t) { ..... } public void consume(Apple a) { ...... } } Apparently I can\'t do that. I can of course implement the dispatch myself, e.g. public class TwoTypesConsumer implements Consumer<Object> { public void consume