multiple-inheritance

Refer base class members from derived class

喜夏-厌秋 提交于 2019-12-08 18:59:07
问题 class A { public: void fa() { } }; class B : public A{ public: void fb() { } }; class C : public A, public B { public: void fc() { //call A::fa(), not B::A::fa(); } }; How to call A::fa() from C::fc() function. GCC warns with direct base A inaccessible in C due to ambiguity , does this mean there is no direct way to refer base class members? 回答1: One option would be to create a stub class that you can use for casting to the right base class subobject: struct A { void fa() { } }; struct B : A

How does the compiler internally solve the diamond problem in C++?

て烟熏妆下的殇ゞ 提交于 2019-12-08 15:29:39
问题 We know that we can solve the diamond problem using virtual inheritance. For example: class Animal // base class { int weight; public: int getWeight() { return weight;}; }; class Tiger : public Animal { /* ... */ }; class Lion : public Animal { /* ... */ }; class Liger : public Tiger, public Lion { /* ... */ }; int main() { Liger lg ; /*COMPILE ERROR, the code below will not get past any C++ compiler */ int weight = lg.getWeight(); } When we compile this code we will get an ambiguity error.

multiple inheritance without virtual inheritance

陌路散爱 提交于 2019-12-08 15:24:29
问题 I am trying to understand multiple inheritance, here is my code: struct A { A() {} static int n; static int increment() { return ++n; } }; int A::n = 0; struct B : public A {}; struct C : public A {}; struct D : public B, C {}; int main() { D d; cout<<d.increment()<<endl; cout<<d.increment()<<endl; } This code works. However, if I change increment() to non-static it will fail. My questions: Why compiler complains ambiguous call of non-static version of increment() , while satisfies with the

C++ multiple inheritance

≯℡__Kan透↙ 提交于 2019-12-08 13:14:31
Please don't question the really odd hierarchy of workers in this code here, I have no idea why anyone would want something like this, but I decided to give myself an exercise in Multiple Inheritance, just to be sure I fully understood it. So here's the result. using namespace std; class Employee { protected: string name; public: string getname() { return name; } void setname(string name2) { name = name2; } Employee(string name2) { name = name2; } Employee(){} }; class Manager : public Employee { public: string getname() { return ("Manager" + name); } Manager(string name2) : Employee(name2){}

Imitate multiple inheritance with overriding

帅比萌擦擦* 提交于 2019-12-08 11:44:46
问题 Last time I found out how to force typescript to see methods copied to class prototype from the other place. The ways were about declaring fields: Fiddle class First { someMethod() { console.log('someMethod from First'); } } function Second() { console.log('Second'); } Second.prototype.doSmth = function () { console.log('doSmth from Second'); } class Both extends First { constructor() { console.log('constructor of Both'); super(); Second.call(this); } doSmth: () => void } for (let key in

How to extend multiple classes in adapter?

偶尔善良 提交于 2019-12-08 11:30:36
I want to implement sections in my list. I have a list of tasks. List has a custom adapter which extends recyclerview swipe adapter as I have implemented swipe gesture to the recyclerview. So now tasks list is shown together with completed and pending tasks. Each list item has a check box which shows task is completed or pending. If check box is checked then task is completed and vise versa. Now I want to make two sections in this with header. One For completed tasks and another for pending tasks. So completed tasks should be shown inside completed section and vise versa. Also if the task is

Multiple inheritance: The derived class gets attributes from one base class only?

被刻印的时光 ゝ 提交于 2019-12-08 09:48:27
I was trying to learn the concepts of multiple-inheritance in Python. Consider a class Derv derived from two classes, Base1 and Base2 . Derv inherits members from the first base class only: class Base1: def __init__(self): self.x=10 class Base2: def __init__(self): self.y=10 class Derv (Base1, Base2): pass d = Derv() print (d.__dict__) The result is { 'x' : 10 } and reversing the order of inheritance gives only { 'y' : 10 } . Shouldn't the derived class inherit attributes from both the base classes? I don't fully understand why it's like this, but I can tell you how to fix it: For some reason,

python multi inheritance with parent classes have different __init__()

爷,独闯天下 提交于 2019-12-08 08:03:23
问题 Here both B and C are derived from A , but with different __init__() parameters. My question is how to write the correct/elegant code here to initialize self.a,self.b,self.c1,self.c2 in the following example? Maybe another question is--is it a good coding practice to do this variable setting in __init()__ function or it is better to use simpler __init__() function, and do set() function for each class later, which seems not as simple as to just do it in __init()__ ? class A(object): __init__

How to extend multiple classes in adapter?

折月煮酒 提交于 2019-12-08 06:40:28
问题 I want to implement sections in my list. I have a list of tasks. List has a custom adapter which extends recyclerview swipe adapter as I have implemented swipe gesture to the recyclerview. So now tasks list is shown together with completed and pending tasks. Each list item has a check box which shows task is completed or pending. If check box is checked then task is completed and vise versa. Now I want to make two sections in this with header. One For completed tasks and another for pending

Lua inheritance

孤者浪人 提交于 2019-12-08 03:40:06
问题 I have two classes in Lua. test1 = {test1Data = 123, id= {0,3}} function test1:hello() print 'HELLO!' end function test1:new (inp) inp = inp or {} setmetatable(inp, self) self.__index = self return inp end test2 = {} function test2:bye () print 'BYE!' end function test2:create_inst( baseClass ) local new_class = {} local class_mt = { __index = new_class } function new_class:create() local newinst = {} setmetatable( newinst, class_mt ) return newinst end if baseClass then setmetatable( new