multiple-inheritance

Mixins vs. Traits

三世轮回 提交于 2019-11-27 10:04:20
What is the difference between Mixins and Traits? According to Wikipedia , Ruby Modules are sort of like traits. How so? 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. There may be the name conflict. Two mixins ( MA and MB ) or traits ( TA and TB ) define method with the same

multiple inheritance: unexpected result after cast from void * to 2nd base class

本秂侑毒 提交于 2019-11-27 09:07:21
My program needs to make use of void* in order to transport data or objects in dynamic invocation situation, so that it can reference data of arbitrary types, even primitive types. However, I recently discovered that the process of down-casting these void* in case of classes with multiple base classes fails and even crashes my program after invoking methods on these down casted pointers even if the memory addresses seem to be correct. The crash happens during access to "vtable". So I have created a small test case, environment is gcc 4.2 on Mac OS X: class Shape { public: virtual int w() = 0;

How to reference a generic return type with multiple bounds

微笑、不失礼 提交于 2019-11-27 07:24:42
I have recently seen that one can declare a return type that is also bounded by an interface. Consider the following class and interface: public class Foo { public String getFoo() { ... } } public interface Bar { public void setBar(String bar); } I can declare a return type like this: public class FooBar { public static <T extends Foo & Bar> T getFooBar() { //some implementation that returns a Foo object, //which is forced to implement Bar } } If I call that method from somewhere, my IDE is telling me that the return type has the method String getFoo() as well as setBar(String) , but only If I

Python's Multiple Inheritance: Picking which super() to call

大兔子大兔子 提交于 2019-11-27 07:23:53
In Python, how do I pick which Parent's method to call? Say I want to call the parent ASDF2's __init__ method. Seems like I have to specify ASDF1 in the super()..? And if I want to call ASDF3's __init__ , then I must specify ASDF2 ?! >>> class ASDF(ASDF1, ASDF2, ASDF3): def __init__(self): super(ASDF1, self).__init__() >>> ASDF() ASDF2's __init__ happened >>> class ASDF(ASDF1, ASDF2, ASDF3): def __init__(self): super(ASDF2, self).__init__() >>> ASDF() ASDF3's __init__ happened Seems bonkers to me. What am I doing wrong? Falmarri That's not what super() is for. Super basically picks one (or all

Triple inheritance causes metaclass conflict… Sometimes

拈花ヽ惹草 提交于 2019-11-27 06:57:54
Looks like I stumbled upon a metaclass hell even when I didn't wanted anything to do with it. I'm writing an app in Qt4 using PySide. I want to separate event-driven part from UI definition, which is generated from Qt Designer files. Hence I create a "controller" classes, but to ease my life I multiple-inherit them anyways. An example: class BaseController(QObject): def setupEvents(self, parent): self.window = parent class MainController(BaseController): pass class MainWindow(QMainWindow, Ui_MainWindow, MainController): def __init__(self, parent=None): super(MainWindow, self).__init__(parent)

Object layout in case of virtual functions and multiple inheritance

久未见 提交于 2019-11-27 06:50:55
I was recently asked in an interview about object layout with virtual functions and multiple inheritance involved. I explained it in context of how it is implemented without multiple inheritance involved (i.e. how the compiler generated the virtual table, insert a secret pointer to the virtual table in each object and so on). It seemed to me that there was something missing in my explanation. So here are questions (see example below) What is the exact memory layout of the object of class C. Virtual tables entries for class C. Sizes (as returned by sizeof) of object of classes A, B and C. (8, 8

When virtual inheritance IS a good design?

亡梦爱人 提交于 2019-11-27 06:45:31
EDIT3: Please be sure to clearly understand what I am asking before answering (there are EDIT2 and lots of comments around). There are (or were) many answers which clearly show misunderstanding of the question (I know that's also my fault, sorry for that) Hi, I've looked over the questions on virtual inheritance ( class B: public virtual A {...} ) in C++, but did not find an answer to my question. I know that there are some issues with virtual inheritance, but what I'd like to know is in which cases virtual inheritance would be considered a good design. I saw people mentioning interfaces like

using declaration in variadic template

狂风中的少年 提交于 2019-11-27 06:41:22
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_visitor<ReturnType>() , Lambdas(lambdas)... { } }; I'm not sure what would be the right syntax of adding

python abstractmethod with another baseclass breaks abstract functionality

独自空忆成欢 提交于 2019-11-27 06:06:45
问题 Consider the following code example import abc class ABCtest(abc.ABC): @abc.abstractmethod def foo(self): raise RuntimeError("Abstract method was called, this should be impossible") class ABCtest_B(ABCtest): pass test = ABCtest_B() This correctly raises the error: Traceback (most recent call last): File "/.../test.py", line 10, in <module> test = ABCtest_B() TypeError: Can't instantiate abstract class ABCtest_B with abstract methods foo However when the subclass of ABCtest also inherits from

How to simulate multiple inheritance in C#

筅森魡賤 提交于 2019-11-27 06:02:29
问题 How can I do this: Class A : DependencyObject {} Class B : DependencyObject {} Class C : A , B {} 回答1: C# does not have multiple inheritance, so the line Class C : A , B {} will never work. You can do similar things with interfaces though, along the lines of interface InterfaceA { void doA(); } class A : InterfaceA { public void doA() {} } interface InterfaceB { void doB(); } class B : InterfaceB { public void doB() {}} class C : InterfaceA, InterfaceB { m_A = new A(); m_B = new B(); public