multiple-inheritance

c++ Multiple parents with same variable name

夙愿已清 提交于 2019-12-05 19:20:10
问题 class A{ protected: int var; }; class B{ protected: int var; }; class C : public A, public B {}; What happens here? Do the variable merges? Can I call one in specific like, B::var = 2, etc. 回答1: You class C will have two variables, B::var and A::var . Outside of C you can access them like this (if you change to public: ), C c; c.A::var = 2; Attempting to access c.var will lead to an error, since there is no field with the name var , only A::var and B::var . Inside C they behave like regular

Overcoming diamond ambiguity in different way

江枫思渺然 提交于 2019-12-05 17:59:41
I know the diamond problem and method to solve it using virtual base class. I tried to solve diamond problem in a different way but did not succeed. I don't know why. #include <iostream> using namespace std; class A { public: void display() { cout << "successfully printed"; } }; class B: public A { }; class C: private A // display() of A will become private member of C { }; class D: public B, public C // private member display() of C should not be inherited { }; int main() { D d; d.display(); return 0; } As the private members are not inherited the class D will not inherit any function from C,

What is the proper approach to swap and copy idiom in virtual inheritance?

瘦欲@ 提交于 2019-12-05 16:26:49
Consider classic virtual inheritance diamond hierarchy. I wonder to know what is the right implementation of copy and swap idiom in such hierarchy. The example is a little artificial - and it is not very smart - as it would play good with default copy semantic for A,B,D classes. But just to illustrate the problem - please forget about the example weaknesses and provide the solution. So I have class D derived from 2 base classes (B<1>,B<2>) - each of B classes inherits virtually from A class. Each class has non trivial copy semantics with using of copy and swap idiom. The most derived D class

Why does C# allow multiple inheritance though interface extension methods but not classes? [closed]

丶灬走出姿态 提交于 2019-12-05 13:58:47
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 7 years ago . I've checked through other questions and surprisingly this question doesn't seem to have been asked. With Extension methods, interfaces provide limited but true implementation multiple inheritance. This brings with it the Diamond problem, the same as with class based multiple inheritance. Why is this better or

C++ : Multiple inheritance with polymorphism

浪子不回头ぞ 提交于 2019-12-05 13:02:36
问题 (pardon the noob question in advance) I have 4 classes: class Person {}; class Student : public Person {}; class Employee : public Person {}; class StudentEmployee : public Student, public Employee {}; Essentially Person is the base class, which are directly subclassed by both Student and Employee . StudentEmployee employs multiple inheritance to subclass both Student and Employee . Person pat = Person("Pat"); Student sam = Student("Sam"); Employee em = Employee("Emily"); StudentEmployee sen

Behaviour of virtual inheritance in this case

匆匆过客 提交于 2019-12-05 07:26:17
问题 I wrote this program with virtual inheritance and I have a few questions. #include<iostream> using namespace std; class B1 { public: B1() { cout << "In B1 constructor\n"; } }; class V1 : public B1 { public: V1() { cout << "In V1 constructor\n"; } }; class D1 : virtual public V1 { public: D1() { cout << "In D1 constructor\n"; } }; class B2 { public: B2() { cout << "In B2 constructor\n"; } }; class B3 { public: B3() { cout << "In B3 constructor\n"; } }; class V2 : public B1, public B2 { public:

Is it better to use `#ifdef` or inheritance for cross-compiling?

守給你的承諾、 提交于 2019-12-05 07:07:11
To follow from my previous question about virtual and multiple inheritance (in a cross platform scenario) - after reading some answers, it has occurred to me that I could simplify my model by keeping the server and client classes, and replacing the platform specific classes with #ifdefs (which is what I was going to do originally). Will using this code be simpler? It'd mean there'd be less files at least! The downside is that it creates a somewhat "ugly" and slightly harder to read Foobar class since there's #ifdefs all over the place. Note that our Unix Foobar source code will never be passed

Multiple derived abstract classes?

我们两清 提交于 2019-12-05 05:35:28
I have to create a Course Management System with course categories: Cooking, sewing and writing courses cooking and writing each have 2 courses (Italian, seafood, creative write and business write). This creates derived abstract: abstract course -> abstract cooking -> concrete seafood The abstract cooking and writing have common fields and some common methods, however they also have abstract methods that are abstract in the base class. Can this be done in C#? If I make the derived abstract class methods abstract Visual Studio says they hide the base class abstract and then the concrete class

Python 3.6.5 “Multiple bases have instance lay-out conflict” when multi-inheritance of classes having __slots__

做~自己de王妃 提交于 2019-12-05 04:46:56
If I run this code, I'v got the subject error message. But why? And how to avoid it getting the C class having its parents slots? class A(): __slots__ = ['slot1'] class B(): __slots__ = ['slot2'] class C(A, B): __slots__ = [] MT-FreeHongKong Simply speak, you just cannot do it. As stated in Documentation , Multiple inheritance with multiple slotted parent classes can be used, but only one parent is allowed to have attributes created by slots (the other bases must have empty slot layouts) - violations raise TypeError. The idea behind __slots__ is to reserve specific slots for each attribute in

Python abc module: Extending both an abstract base class and an exception-derived class leads to surprising behavior

我怕爱的太早我们不能终老 提交于 2019-12-05 04:17:23
Extending both an abstract base class and a class derived from "object" works as you would expect: if you you haven't implemented all abstract methods and properties, you get an error. Strangely, replacing the object-derived class with an class that extends "Exception" allows you to create instances of classes which do not implement all the required abstract methods and properties. For example: import abc # The superclasses class myABC( object ): __metaclass__ = abc.ABCMeta @abc.abstractproperty def foo(self): pass class myCustomException( Exception ): pass class myObjectDerivedClass( object )