multiple-inheritance

abstract base classes, multiple inheritence, and common pure virtual methods

半腔热情 提交于 2019-12-20 03:46:09
问题 The following test code seems to indicate that if a class has two abstract base classes with common pure virtual methods, then these methods are "shared" in the derived class. #include <iostream> #include <string> using namespace std; struct A { virtual string do_a() const = 0; virtual void set_foo(int x) = 0; virtual int get_foo() const = 0; virtual ~A() {} }; struct B { virtual string do_b() const = 0; virtual void set_foo(int x) = 0; virtual int get_foo() const = 0; virtual ~B() {} };

Python multiple inheritance constructor not called when using super()

旧巷老猫 提交于 2019-12-20 02:09:15
问题 Consider the following code: class A(object): def __init__(self): pass class B(object): def __init__(self): self.something = 'blue' def get_something(self): return self.something class C(A,B): def __init__(self): super().__init__() print(self.get_something()) and then do: c = C() which results in something like this: AttributeError: 'C' object has no attribute 'something' I suppose this happens due to the constructor of B not being called when using super(). Is there a way to achieve the

Python multiple inheritance constructor not called when using super()

主宰稳场 提交于 2019-12-20 02:09:07
问题 Consider the following code: class A(object): def __init__(self): pass class B(object): def __init__(self): self.something = 'blue' def get_something(self): return self.something class C(A,B): def __init__(self): super().__init__() print(self.get_something()) and then do: c = C() which results in something like this: AttributeError: 'C' object has no attribute 'something' I suppose this happens due to the constructor of B not being called when using super(). Is there a way to achieve the

Is there a way to prevent a class from being derived from twice using a static assert and type trait?

流过昼夜 提交于 2019-12-20 01:06:58
问题 I realize this is a contrived example, but I want a compile check to prevent this... class A {}; class B : public A {}; class C : public A {}; class D : public B, public C { BOOST_STATIC_ASSERT((is_base_of_once<A,D>::value)) }; 回答1: The following should work: BOOST_STATIC_ASSERT(((A*)(D*)0 == 0)) If A exists twice, this should rise an ambiguity error, while otherwise the test will always succeed (because it compares two null pointers). 回答2: When I try to derive a class twice as you have here

How to override a function in another base class?

血红的双手。 提交于 2019-12-19 07:12:14
问题 I'm not exactly sure the terminology to use, but here's my example: class Base { public: virtual void test() = 0; }; class Mixin { public: virtual void test() { } }; class Example : public Base, public Mixin { }; int main(int argc, char** argv) { Example example; example.test(); return 0; } I want my Mixin class to implement the pure virtual function Base::test , but when I compile this, it says: test.cpp: In function ‘int main(int, char**)’: test.cpp:15:13: error: cannot declare variable

How to override a function in another base class?

末鹿安然 提交于 2019-12-19 07:11:04
问题 I'm not exactly sure the terminology to use, but here's my example: class Base { public: virtual void test() = 0; }; class Mixin { public: virtual void test() { } }; class Example : public Base, public Mixin { }; int main(int argc, char** argv) { Example example; example.test(); return 0; } I want my Mixin class to implement the pure virtual function Base::test , but when I compile this, it says: test.cpp: In function ‘int main(int, char**)’: test.cpp:15:13: error: cannot declare variable

virtual method table for multiple-inheritance

ぐ巨炮叔叔 提交于 2019-12-19 07:10:12
问题 I'm reading this article "Virtual method table" Example in the above article: class B1 { public: void f0() {} virtual void f1() {} int int_in_b1; }; class B2 { public: virtual void f2() {} int int_in_b2; }; class D : public B1, public B2 { public: void d() {} void f2() {} // override B2::f2() int int_in_d; }; B2 *b2 = new B2(); D *d = new D(); In the article, the author introduces that the memory layout of object d is like this: d: D* d--> +0: pointer to virtual method table of D (for B1) +4:

Multiple Inheritance Template Class

假装没事ソ 提交于 2019-12-19 06:29:19
问题 class messageA { }; class messageB { }; template<class T> class queue { public: virtual ~queue() {} void submit(T& x) {} }; class A : public queue<messageA>, public queue<messageB> { }; int main() { A aa; aa.submit(messageA()); aa.submit(messageB()); } My first thought is, the above code should be fine, as class A will contains 2 overloaded submit functions, which will accept messageA and messageB object. However, the compiler gives me the following error : May I know why there is an

Diamond of death and Scope resolution operator (c++)

荒凉一梦 提交于 2019-12-19 05:16:08
问题 I have this code (diamond problem): #include <iostream> using namespace std; struct Top { void print() { cout << "Top::print()" << endl; } }; struct Right : Top { void print() { cout << "Right::print()" << endl; } }; struct Left : Top { void print() { cout << "Left::print()" << endl; } }; struct Bottom: Right, Left{}; int main() { Bottom b; b.Right::Top::print(); } I want to call print() in Top class. When I try to compile it I get error: 'Top' is an ambiguous base of 'Bottom' on this line: b

C++ virtual inheritance initializer list

故事扮演 提交于 2019-12-19 01:37:25
问题 in the following code: class A { public: int x; A(int x):x(x){} }; class B: public virtual A { public: B(int x):A(x){} }; class C: public virtual A { public: C(int x):A(x){} }; class D: public B, public C { public: D(int x):B(x++), C(x++), A(x++){} }; two questions: Why do I need to add A(...) in D's initializer list? D(int x):B(x++), C(x++), A(x++){} and D(int x):A(x++), B(x++), C(x++){} both give the same result with cout<<D(10).x , why? 回答1: Why do I need to add A(...) in D's initializer