multiple-inheritance

Where is the “virtual” keyword necessary in a complex multiple inheritance hierarchy?

回眸只為那壹抹淺笑 提交于 2019-12-02 15:59:46
I understand the basics of C++ virtual inheritance. However, I'm confused about where exactly I need to use the virtual keyword with a complex class hierarchy. For example, suppose I have the following classes: A / \ B C / \ / \ D E F \ / \ / G H \ / I If I want to ensure that none of the classes appear more than once in any of the subclasses, which base classes need to be marked virtual ? All of them? Or is it sufficient to use it only on those classes that derive directly from a class that may otherwise have multiple instances (i.e. B, C, D, E and F; and G and H (but only with the base class

If one class is derived from another that is derived from Object, is that “multiple inheritence”

拜拜、爱过 提交于 2019-12-02 12:56:22
The fact about Java is that it does not support the multiple inheritance. But I have a question that the base class of all java classes is Object. Now we have two classes : Class A and Class B. B is inherited from A then the Base class of B would be the Object and A , so here, the multiple inheritance took place. Anyone will please help me to clear my doubt? The concept of Multiple Inheritance means, that you can have a class A, B and C where A is derived from B and C like this: class A extends B, C {} This is not possible in Java. What you describe is a straightforwar inheritance with a

Can you re-make a method abstract in the inheritance tree?

浪尽此生 提交于 2019-12-02 09:23:50
EDIT: To be clear: The fact that the design is quite ugly is not the point. The point is, that the design is there and I am in the situation to have to add another sub-class of FlyingMotorizedVehicle which would not work as expected if I forgot to add the foo(...) . So I just was wondering if I could redefine it as abstract. I am right now facing a quite weird inheritance situation. Lets say, I have three classes, Vehicle , MotorizedVehicle and FlyingMotorizedVehicle as well as a bunch of classes Airplane , Helicopter , ...: public abstract class Vehicle { abstract Something doStuff(...);

Abstract classes and Multiple Inheritance

瘦欲@ 提交于 2019-12-02 07:05:24
We can achieve the same functionality as interfaces by using abstract classes, So why java doesn't allow the following code? abstract class Animals { public abstract void run(); } abstract class Animals1 { public abstract void run1(); } class Dog extends Animals,Animals1 { public void run() {System.out.println("Run method");} public void run1() {System.out.println("Run1 method");} } I know that multiple inheritance can be achieved by using only interfaces but the above code does the same thing as the interfaces would have done it. This is not allowed because you can do more than this with

C++ Resolving the diamond problem

喜欢而已 提交于 2019-12-02 04:56:04
问题 Couldn't the diamond problem be resolved just by using the first inherited declaration found? I mean, public class A { public virtual int getInt(); }; public class B : public A { public int getInt() {return 6;} }; public class C : public A { public int getInt() {return 7;} }; public class D: public B, public C {}; for class D , since B is listed first, couldn't we just by default (when it's ambiguous) use B::getInt() if D::getInt() is called? Kind of how the PATH environment variable works in

C++ Resolving the diamond problem

雨燕双飞 提交于 2019-12-02 02:41:18
Couldn't the diamond problem be resolved just by using the first inherited declaration found? I mean, public class A { public virtual int getInt(); }; public class B : public A { public int getInt() {return 6;} }; public class C : public A { public int getInt() {return 7;} }; public class D: public B, public C {}; for class D , since B is listed first, couldn't we just by default (when it's ambiguous) use B::getInt() if D::getInt() is called? Kind of how the PATH environment variable works in UNIX and other OS's; if two things exist with the same name in different locations in the PATH

C++, statically detect base classes with differing addresses?

雨燕双飞 提交于 2019-12-02 02:26:20
If I have a derived class with multiple bases, each this pointer for each base will be different from that of the derived object's this pointer, except for one. Given two types in an inheritance hierarchy, I'd like to detect at compile time whether they share the same this pointer. Something like this should work, but doesn't: BOOST_STATIC_ASSERT(static_cast<Base1*>((Derived *)0xDEADBEEF) == (Derived*)0xDEADBEEF); Because it needs to be an 'integral constant expression' and only integer casts are allowed in those according to the standard (which is stupid, because they only need compile time

C++, statically detect base classes with differing addresses?

我怕爱的太早我们不能终老 提交于 2019-12-02 01:42:44
问题 If I have a derived class with multiple bases, each this pointer for each base will be different from that of the derived object's this pointer, except for one. Given two types in an inheritance hierarchy, I'd like to detect at compile time whether they share the same this pointer. Something like this should work, but doesn't: BOOST_STATIC_ASSERT(static_cast<Base1*>((Derived *)0xDEADBEEF) == (Derived*)0xDEADBEEF); Because it needs to be an 'integral constant expression' and only integer casts

abstract base classes, multiple inheritence, and common pure virtual methods

亡梦爱人 提交于 2019-12-02 00:09:45
The following test code seems to indicate that if a class has two abstract base classes with common pure virtual methods, then these methods are "shared" in the derived class. #include <iostream> #include <string> using namespace std; struct A { virtual string do_a() const = 0; virtual void set_foo(int x) = 0; virtual int get_foo() const = 0; virtual ~A() {} }; struct B { virtual string do_b() const = 0; virtual void set_foo(int x) = 0; virtual int get_foo() const = 0; virtual ~B() {} }; struct C : public A, public B { C() : foo(0) {} string do_a() const { return "A"; } string do_b() const {

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

我只是一个虾纸丫 提交于 2019-12-01 23:51:31
问题 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