multiple-inheritance

Inherit interfaces which share a method name

我只是一个虾纸丫 提交于 2019-11-26 18:33:41
There are two base classes have same function name. I want to inherit both of them, and over ride each method differently. How can I do that with separate declaration and definition (instead of defining in the class definition)? #include <cstdio> class Interface1{ public: virtual void Name() = 0; }; class Interface2 { public: virtual void Name() = 0; }; class RealClass: public Interface1, public Interface2 { public: virtual void Interface1::Name() { printf("Interface1 OK?\n"); } virtual void Interface2::Name() { printf("Interface2 OK?\n"); } }; int main() { Interface1 *p = new RealClass(); p-

Can an interface extend multiple interfaces in Java?

扶醉桌前 提交于 2019-11-26 18:27:30
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? Suresh Atta 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 interfaces. What if two interfaces have a method defining the same name and signature? There is a

Resolving metaclass conflicts

﹥>﹥吖頭↗ 提交于 2019-11-26 18:23:01
问题 I need to create a class that uses a different base class depending on some condition. With some classes I get the infamous: TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases One example is sqlite3 , here is a short example you can even use in the interpreter: >>> import sqlite3 >>> x = type('x', (sqlite3,), {}) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: metaclass conflict:

Mixins vs. Traits

喜你入骨 提交于 2019-11-26 17:54:39
问题 What is the difference between Mixins and Traits? According to Wikipedia, Ruby Modules are sort of like traits. How so? 回答1: Mixins may contain state, (traditional) traits don't. Mixins use "implicit conflict resolution", traits use "explicit conflict resolution" Mixins depends on linearization, traits are flattened. Lecture about traits ad 1. In mixins you can define instance variables. Traits do not allow this. The state must be provided by composing class (=class using the traits) ad 2.

Alternative of Multiple inheritance in Java

孤者浪人 提交于 2019-11-26 17:49:12
问题 I have created two beans class BackPageBean{ String backPage = null; : : : } class InformationMessageBean{ String informationMessage = null; : : : } Now, if a class is backpage aware then it will extend backPageBean, or if it need to show some kind of message then it extends InformationMessageBean. class BackPageAware extends backPageBean{ } class InfoMessAware extends InformationMessageBean{ } someFunction () { if ( theObject instanceOf backPageBean ) { prepareTheBackPage ( theObject

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

泄露秘密 提交于 2019-11-26 17:46:42
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(), return S_OK } The documents usually say that if I do the latter I will violate the requirement that any call

using declaration in variadic template

寵の児 提交于 2019-11-26 17:35:35
问题 This question is inspired in the following solution to multiple inheritance overloading pseudo-ambiguity, which is a nice way to implement lambda visitors for boost::variant as proposed in this answer: I want to do something like the following: template <typename ReturnType, typename... Lambdas> struct lambda_visitor : public boost::static_visitor<ReturnType>, public Lambdas... { using Lambdas...::operator(); //<--- doesn't seem to work lambda_visitor(Lambdas... lambdas) : boost::static

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

走远了吗. 提交于 2019-11-26 17:31:14
http://en.wikipedia.org/wiki/Diamond_problem I know what it means, but what steps can I take to avoid it? 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: class A {}; class B : virtual public A {}; class C : virtual public A {}; class D : public B, public C {};

C++ multiple inheritance function call ambiguity

这一生的挚爱 提交于 2019-11-26 17:28:44
问题 I have a basic question related to multiple inheritance in C++. If I have a code as shown below: struct base1 { void start() { cout << "Inside base1"; } }; struct base2 { void start() { cout << "Inside base2"; } }; struct derived : base1, base2 { }; int main() { derived a; a.start(); } which gives the following compilation error: 1>c:\mytest.cpp(41): error C2385: ambiguous access of 'start' 1> could be the 'start' in base 'base1' 1> or could be the 'start' in base 'base2' Is there no way to

Why can't I use inheritance to implement an interface in C++? [duplicate]

时光毁灭记忆、已成空白 提交于 2019-11-26 17:24:42
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Implementing abstract class members in a parent class Why does C++ not let baseclasses implement a derived class' inherited interface? Considering these objects: struct A { virtual void foo() = 0; }; struct B { void foo() { /* neat implementation */ } }; I wonder why --compiler-wise-- the following object is considered abstract: struct C : B, A { using B::foo; // I tried my best to make the compiler happy }; The