diamond-problem

How to have multiple traits of same base trait implement the same method

自闭症网瘾萝莉.ら 提交于 2021-01-29 11:21:19
问题 I have a scenario with multiple traits inheriting from a fixed base trait. The base trait has an abstract method that each needs to implement. The class using these traits also needs to implement this method: trait A { def f: Any } trait B1 extends A { val i: Int override def f: Any = println("B1: " + i) } trait B2 extends A { val j: Int override def f: Any = println("B2: " + j) } class C extends A with B1 with B2 { val i = 1 val j = 2 override def f: Any = { super.f println("C::f") } } Then

How to avoid parallel class hierarchy in python

余生颓废 提交于 2020-11-29 08:54:12
问题 I've been running into a weird little smell in my python code lately and I think it has something to do with parallel inheritance. Here is a small example I concocted: class DogHabits: def __init__(self): self.habits = ['lick butt'] class GermanShepherdHabits(DogHabits): def __init__(self): super().__init__() self.habits.extend(['herd sheep']) class LabradorHabits(DogHabits): def __init__(self): super().__init__() self.habits.extend(['hunt', 'pee on owner']) class Dog: def __init__(self):

Do Derived1::Base and Derived2::Base refer to the same type?

本小妞迷上赌 提交于 2020-05-12 11:38:01
问题 MSVC, Clang and GCC disagree on this code: struct Base { int x; }; struct Der1 : public Base {}; struct Der2 : public Base {}; struct AllDer : public Der1, public Der2 { void foo() { Der1::Base::x = 5; } }; Godbolt GCC: <source>: In member function 'void AllDer::foo()': <source>:10:21: error: 'Base' is an ambiguous base of 'AllDer' 10 | Der1::Base::x = 5; | ^ Compiler returned: 1 Clang gives a similar error, and MSVC gives no error. Who is right here? I suppose this is covered in [class

Diamond inheritance with mixed inheritance modifers (protected / private / public)

守給你的承諾、 提交于 2020-01-24 05:43:07
问题 let's say we have class A,B,C,D where A is base, B,C are between and D is derived in diamond model. NOTE: class B inherits virtualy class A in private mode, class C inherita virtualy class A in protected mode. class A { public: int member; // note this member }; class B : virtual private A // note private { }; class C : virtual protected A // note protected { }; class D : public B, // doesn't metter public or whatever here public C { }; int main() { D test; test.member = 0; // WHAT IS member?

How to resolve the diamond issue in PHP?

怎甘沉沦 提交于 2020-01-16 00:06:40
问题 I have searched for solutions to the diamond problem but the only one I have found is using traits which I can't use in my case, so I'm asking here to see if anyone has an alternative solution. I have a base class Controller (I cannot change this class) and have two subclasses SecurityController and DevController . Each of these subclasses introduces methods which also use methods inside the base class. I then have a final class ApplicationController which, ideally, would extend both

How to resolve the diamond issue in PHP?

£可爱£侵袭症+ 提交于 2020-01-16 00:06:09
问题 I have searched for solutions to the diamond problem but the only one I have found is using traits which I can't use in my case, so I'm asking here to see if anyone has an alternative solution. I have a base class Controller (I cannot change this class) and have two subclasses SecurityController and DevController . Each of these subclasses introduces methods which also use methods inside the base class. I then have a final class ApplicationController which, ideally, would extend both

Why is the diamond case with its common ancestor used to explain Java multiple inheritance issue, instead of two unrelated parent classes?

点点圈 提交于 2020-01-01 07:45:50
问题 This question might sound weird to Java people but if you try to explain this, it would be great. In these days I am clearing some of Java's very basic concept. So I come to Inheritance and Interface topic of Java. While reading this I found that Java does not support Multiple Inheritance and also understood that, what I am not able to understand that why everywhere Diamond figure issue(At least 4 class to create diamond) is discussed to explain this behavior, Can't we understand this issue

Consequences of changing inheritance to virtual?

怎甘沉沦 提交于 2019-12-30 03:13:25
问题 I'm working on a huge project that I didn't start. My task is to add some additional functionality to what already is there. I'm in a situation where I have to use virtual inheritance because I have a diamond model. The situation is depicted in the following illustration: Base class / \ / \ My new class A class that was there before (OldClass) \ / \ / \ / \ / My other new class For this to work, both the classes in the middle have to inherit from the base through public virtual instead of

Virtual Extension Methods in upcoming Java 8 release

牧云@^-^@ 提交于 2019-12-30 03:05:21
问题 When I see code snippets like interface A { void a(); void b() default { System.out.println("b"); }; void c() final { System.out.println("c"); }; } I have one question. Haven't we already got enough sh*t in Java? Why one might need this? 回答1: I suggest you to look at this conference : http://medianetwork.oracle.com/media/show/16999 This explain everything. The most interesting thing to do is to allow an interface to evolve without rewritting your whole codebase. This is key to allow a big