multiple-inheritance

Multiple inheritance with one base class

懵懂的女人 提交于 2019-11-26 23:44:10
问题 (Removed original text as it is unrelated to the current question which has already been answered. See revisions.) Here is my example test.hpp (simplified): class House { private: int nWindows; public: House(int nWindows); int getNumberOfWindows(); }; class PaintedHouse : public virtual House { private: int colorCode; public: PaintedHouse(int nWindows, int colorCode); int getColorCode(); }; class OccupiedHouse : public virtual House { private: int nPeople; public: OccupiedHouse(int nWindows,

Understanding virtual base classes and constructor calls

别说谁变了你拦得住时间么 提交于 2019-11-26 23:20:32
问题 I'm a bit confused about how virtual base classes work. In particular, I was wondering how the constructor of the base class gets called. I wrote an example to understand it: #include <cstdio> #include <string> using std::string; struct A{ string s; A() {} A(string t): s(t) {} }; struct B: virtual public A{ B(): A("B"){} }; struct C: virtual public A {}; struct D: public B, public C {}; struct E: public C, public B {}; struct F: public B {}; int main(){ D d; printf("\"%s\"\n",d.s.c_str()); E

C++11: Disambiguate class-member in multiple inheritance

夙愿已清 提交于 2019-11-26 22:59:18
Suppose I have this variadic base class-template: template <typename ... Types> class Base { public: // The member foo() can only be called when its template // parameter is contained within the Types ... pack. template <typename T> typename std::enable_if<Contains<T, Types ...>::value>::type foo() { std::cout << "Base::foo()\n"; } }; The foo() member can only be called when its template-parameter matches at least one of the parameters of Base (the implementation of Contains is listed at the bottom at this post): Base<int, char>().foo<int>(); // fine Base<int, char>().foo<void>(); // error Now

Virtual tables and memory layout in multiple virtual inheritance

半世苍凉 提交于 2019-11-26 22:41:50
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 contains, where exactly each of them is placed? Which of virtual tables are shared with virtual table of C?

Virtual inheritance in C++

老子叫甜甜 提交于 2019-11-26 21:33:57
问题 I found this in a website while reading about virtual inheritance in c++ When multiple inheritance is used, it is sometimes necessary to use virtual inheritance. A good example for this is the standard iostream class hierarchy: //Note: this is a simplified description of iostream classes class ostream: virtual public ios { /*..*/ } class istream: virtual public ios { /*..*/ } class iostream : public istream, public ostream { /*..*/ } //a single ios inherited How does C++ ensure that only a

How does Python's “super” do the right thing?

﹥>﹥吖頭↗ 提交于 2019-11-26 21:30:15
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 super in the parent classes the correct order. Consider this example code: #!/usr/bin/python class A

Class extending more than one class Java?

拈花ヽ惹草 提交于 2019-11-26 21:14:26
问题 I know that a class can implement more than one interface, but is it possible to extend more than one class? For example I want my class to extend both TransformGroup and a class I created. Is this possible in Java? Both statements class X extends TransformGroup extends Y and class X extends TransformGroup, Y receive an error. And if it is not possible, why? TransformGroup extends Group but I guess it also extends Node since it inherits fields from Node and it can be passed where a Node

Multiple Inheritance in java

ⅰ亾dé卋堺 提交于 2019-11-26 20:48:13
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 ? 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, that's the only good MI will ever do you. I have read that most programmers don't use multiple inheritance in a

How to avoid infinite recursion with super()?

我的梦境 提交于 2019-11-26 18:53:11
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? When instantiating C calls B.__init__ , self.__class__ will still be C, so the super() call brings it back to B. When calling super(), use the class names directly. So in B, call super(B, self) , rather than super(self.__class__, self) (and

Does C# support multiple inheritance?

不羁岁月 提交于 2019-11-26 18:51:33
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. Nope, use interfaces instead! ^.^ Asad Butt 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 { } Possible ways to inherit: class SomeClass : A, B { } // from multiple Interface(s) class SomeClass : Base, B