multiple-inheritance

In C++, why is the address changed when the pointer is converted?

自作多情 提交于 2019-12-01 21:47:09
Following is the code: #include <iostream> using namespace std; class B1 { public: virtual void f1() { cout << "B1\n"; } }; class B2 { public: virtual void f1() { cout << "B2\n"; } }; class D : public B1, public B2 { public: void f1() { cout << "OK\n" ; } }; int main () { D dd; B1 *b1d = &dd; B2 *b2d = &dd; D *ddd = &dd; cout << b1d << endl; cout << b2d << endl; cout << ddd << endl; b1d -> f1(); b2d -> f1(); ddd -> f1(); } The output is : 0x79ffdf842ee0 0x79ffdf842ee8 0x79ffdf842ee0 OK OK OK This looks confusing to me, because I expected b1d and b2d would be the same as both of them point to

Python multiple inheritance constructor not called when using super()

我只是一个虾纸丫 提交于 2019-12-01 20:59:36
Consider the following code: class A(object): def __init__(self): pass class B(object): def __init__(self): self.something = 'blue' def get_something(self): return self.something class C(A,B): def __init__(self): super().__init__() print(self.get_something()) and then do: c = C() which results in something like this: AttributeError: 'C' object has no attribute 'something' I suppose this happens due to the constructor of B not being called when using super(). Is there a way to achieve the correct behavior with Python 3? Superclasses should use super if their subclasses do. If you add the super(

Need multiple inheritance functionality in C#. What am I doing wrong?

牧云@^-^@ 提交于 2019-12-01 20:55:37
问题 class UDPClient { } class LargeSimulator { } class RemoteLargeSimulatorClient : UDPClient, LargeSimulator { } The saying goes, if you need multiple inheritance, your design is off. How would I get this done in C# without having to implement anything? 回答1: You can only inherent from a single base class in C#. However, you can implement as many Interfaces as you'd like. Combine this fact with the advent of Extension Methods and you have a (hacky) work-around. 回答2: C# only allows single

python: super()-like proxy object that starts the MRO search at a specified class

不想你离开。 提交于 2019-12-01 19:52:12
According to the docs, super(cls, obj) returns a proxy object that delegates method calls to a parent or sibling class of type cls I understand why super() offers this functionality, but I need something slightly different: I need to create a proxy object that delegates methods calls (and attribute lookups) to class cls itself; and as in super , if cls doesn't implement the method/attribute, my proxy should continue looking in the MRO order (of the new not the original class). Is there any function I can write that achieves that? Example: class X: def act(): #... class Y: def act(): #... class

Is there a way to prevent a class from being derived from twice using a static assert and type trait?

孤街浪徒 提交于 2019-12-01 19:35:56
I realize this is a contrived example, but I want a compile check to prevent this... class A {}; class B : public A {}; class C : public A {}; class D : public B, public C { BOOST_STATIC_ASSERT((is_base_of_once<A,D>::value)) }; The following should work: BOOST_STATIC_ASSERT(((A*)(D*)0 == 0)) If A exists twice, this should rise an ambiguity error, while otherwise the test will always succeed (because it compares two null pointers). When I try to derive a class twice as you have here it does not even compile. (duplicate base type) If you really want to, you an test both your base classes: class

Need multiple inheritance functionality in C#. What am I doing wrong?

断了今生、忘了曾经 提交于 2019-12-01 19:02:06
class UDPClient { } class LargeSimulator { } class RemoteLargeSimulatorClient : UDPClient, LargeSimulator { } The saying goes, if you need multiple inheritance, your design is off. How would I get this done in C# without having to implement anything? You can only inherent from a single base class in C#. However, you can implement as many Interfaces as you'd like. Combine this fact with the advent of Extension Methods and you have a (hacky) work-around. C# only allows single inheritance, though you can inherit from as many interfaces as you wish. You could pick just one class to inherit from,

python class methods and inheritance

六眼飞鱼酱① 提交于 2019-12-01 18:59:52
I would expect the following code to print 012345 but it prints 012012. Why? I would expect the calls to incr to be accessing the same variables since they are inherited from the same class but they are clearly different variables. class a(object): var = 0 @classmethod def incr(cls): print cls.var cls.var+=1 class b(a): def func(self): super(b,self).incr() class c(a): def func(self): super(c,self).incr() t = a() t1 = b() t2 = c() t1.func() t1.func() t1.func() t2.func() t2.func() t2.func() They are inherited from the same class, but the cls passed to the classmethod via super is the current

python class methods and inheritance

旧街凉风 提交于 2019-12-01 17:41:08
问题 I would expect the following code to print 012345 but it prints 012012. Why? I would expect the calls to incr to be accessing the same variables since they are inherited from the same class but they are clearly different variables. class a(object): var = 0 @classmethod def incr(cls): print cls.var cls.var+=1 class b(a): def func(self): super(b,self).incr() class c(a): def func(self): super(c,self).incr() t = a() t1 = b() t2 = c() t1.func() t1.func() t1.func() t2.func() t2.func() t2.func() 回答1

Doubts in Multiple inheritance in .net

若如初见. 提交于 2019-12-01 14:21:47
We know that all the classes are inherited from the object class in .net. Say we create a class named ClassA. Then we create another class named ClassB that is inherited from ClassA. Isn't this multiple inheritance, because ClassB inherited from both Object class and ClassA? Doesn't this break the rule that C#.net doesn't support multiple inheritance? No, you do not break the rule. Since ClassA is an object, it does not mean that you're inheriting from 2 different classes. You're inheriting from ClassA, and thereby taking all the characteristics from the 'inheritance chain' with it. You're not

Multiple Inheritance

浪尽此生 提交于 2019-12-01 14:19:58
#include<iostream> using namespace std; class A { int a; int b; public: void eat() { cout<<"A::eat()"<<endl; } }; class B: public A { public: void eat() { cout<<"B::eat()"<<endl; } }; class C: public A { public: void eat() { cout<<"C::eat()"<<endl; } }; class D: public B, C { }; int foo(A *ptr) { ptr->eat(); } main() { D obj; foo(&(obj.B)); //error. How do i call with D's B part. } The above foo call is a compile time error. I want to call foo with obj's B part without using virtual inheritance. How do i do that. Also, in case of virtual inheritance, why the offset information need to be