multiple-inheritance

How does using interfaces overcome the problem of multiple inheritance in C#?

谁都会走 提交于 2019-11-27 02:44:23
问题 I understand that C# does not support multiple inheritance, and that the solution is to use interfaces instead. But what I don't understand is why interfaces doesn't create the diamond problem the same way as multiple inheritance would. How does using interfaces avoid the pitfalls of multiple inheritance? 回答1: One class may implement any number of interfaces, even if those interfaces extend other interfaces as well. Multiple inheritance is not possible only with classes . // This is not

C++ inherit from multiple base classes with the same virtual function name

梦想的初衷 提交于 2019-11-27 02:14:20
I tried this code: class A { virtual void foo() = 0; }; class B { virtual void foo() = 0; }; class C : public A, public B { //virtual void A::foo(){} //virtual void B::foo(){} virtual void A::foo(); virtual void B::foo(); }; void C::A::foo(){} void C::B::foo(){} int main() { C c; return 0; } It is OK when using the commented part, but when I try to write the definitions outside the class declaration, the compiler reports errors. I am using the MSVC11 compiler, does anyone know how to write this? I need to move the code into the cpp file. Thank you~~ dyp A function overrides a virtual function

Multiple inheritance on Java interfaces

[亡魂溺海] 提交于 2019-11-27 02:05:35
问题 I thought multiple inheritance was always illegal in Java, but this code compiles: public interface A { void a(); } public interface B { void b(); } public interface AB extends A, B { } Would having an empty interface such as AB be considered a bad practice? Is there a way to achieve something similar while avoiding the empty interface (using generics or otherwise)? Note: I'm not asking how to simulate multiple inheritance via interfaces. I realize I could do the following: public class

Mixing virtual and non-virtual inheritance of a base class

╄→гoц情女王★ 提交于 2019-11-27 02:04:53
问题 This is the code: struct Biology { Biology() { cout << "Biology CTOR" << endl; } }; struct Human : Biology { Human() { cout << "Human CTOR" << endl; } }; struct Animal : virtual Biology { Animal() { cout << "Animal CTOR" << endl; } }; struct Centaur : Human, Animal { Centaur() { cout << "Centaur CTOR" << endl; } }; int main() { Centaur c; return 0; } This code prints: Biology CTOR Biology CTOR Human CTOR Animal CTOR Centaur CTOR Why? Since we create a Centaur object, we start from building

Why is multiple inheritance not supported in most of programming language?

喜夏-厌秋 提交于 2019-11-27 01:57:42
问题 Why is multiple inheritance not supported in most of programming language? I could really use this feature to develop different layout of application? 回答1: Multiple inheritance is useful in many situations as a developer, but it greatly increases the complexity of the language, which makes life harder for both the compiler developers and the programmers. One problem occurs when two parent classes have data members or methods of the same name. It is difficult to resolve which is being

A use for multiple inheritance?

旧街凉风 提交于 2019-11-27 01:44:14
Can anyone think of any situation to use multiple inheritance? Every case I can think of can be solved by the method operator AnotherClass() { return this->something.anotherClass; } Erik Funkenbusch Most uses of full scale Multiple inheritance are for mixins. As an example: class DraggableWindow : Window, Draggable { } class SkinnableWindow : Window, Skinnable { } class DraggableSkinnableWindow : Window, Draggable, Skinnable { } etc... In most cases, it's best to use multiple inheritance to do strictly interface inheritance. class DraggableWindow : Window, IDraggable { } Then you implement the

'Inaccessible direct base' caused by multiple inheritance

杀马特。学长 韩版系。学妹 提交于 2019-11-27 01:35:57
Spoiler alert: Maybe a stupid question. :) #include <iostream> using namespace std; class Base { public: virtual void YourMethod(int) const = 0; }; class Intermediate : private Base { public: virtual void YourMethod(int i) const { cout << "Calling from Intermediate" << i << "\n"; } }; class Derived : private Intermediate, public Base { public: void YourMethod(int i) const { cout << "Calling from Derived : " << i << "\n"; } }; int main() { } Can someone Explain to me why this throws the compiler warning : main.cpp:21: warning: direct base ‘Base’ inaccessible in ‘Derived’ due to

How can I require a method argument in Java to implement multiple interfaces?

不想你离开。 提交于 2019-11-27 00:59:47
问题 It's legal to do this in Java: void spew(Appendable x) { x.append("Bleah!\n"); } How can I do this (syntax not legal): void spew(Appendable & Closeable x) { x.append("Bleah!\n"); if (timeToClose()) x.close(); } I would like if possible to force callers to use objects that are both Appendable and Closeable, without requiring a specific type. There are multiple standard classes that do this, e.g. BufferedWriter, PrintStream, etc. If I define my own interface interface AppendableAndCloseable

Diamond inheritance (C++)

隐身守侯 提交于 2019-11-27 00:58:38
I know that having diamond inheritance is considered bad practice. However, I have 2 cases in which I feel that diamond inheritance could fit very nicely. I want to ask, would you recommend me to use diamond inheritance in these cases, or is there another design that could be better. Case 1: I want to create classes that represent different kinds of "Actions" in my system. The actions are classified by several parameters: The action can be "Read" or "Write". The action can be with delay or without delay (It is not just 1 parameter. It changes the behavior significantly). The action's "flow

How can interfaces replace the need for multiple inheritance when have existing classes

筅森魡賤 提交于 2019-11-27 00:30:44
First of all... Sorry for this post. I know that there are many many posts on stackoverflow which are discussing multiple inheritance. But I already know that Java does not support multiple inheritance and I know that using interfaces should be an alternative. But I don't get it and see my dilemma: I have to make changes on a very very large and complex tool written in Java. In this tool there is a data structure built with many different class objects with a linked member hierarchy. Anyway... I have one class Tagged which has multiple methods and returns an object tag depending on the object