multiple-inheritance

Java abstract class implements an interface, both have the same method

泄露秘密 提交于 2019-12-06 05:56:29
问题 While looking at some OOP materials, I thought of this question which confused me a little bit: Consider having the following interface,abstract class, and a concrete class: package one; public interface A { void doStuff(); } package one; public abstract class B implements A { public abstract void doStuff(); } class C extends B{ public void doStuff() { } } Class C won't compile unless it provides an implementation for method doStuff() . The question here: 1-Is doStuff() method in class C an

Why can't I extend an interface “generic method” and narrow its type to my inherited interface “class generic”?

混江龙づ霸主 提交于 2019-12-06 05:46:00
I show an example of what I mean which, is easier. Imagine the generic type C means Color type: So for visual simplification assume C is C extends Color interface Screen { <C> Background<C> render(Plane<C> plane); } interface MonochromeScreen<C> extends Screen{ @Override Background<C> render(Plane<C> plane); } This would throw a name clash compilation error explaining that both have the same type erasure but are not overridable. But I cannot understand why we could not simply allow overriding the signature as long as it is more restrictive. I mean, after all, the only difference is the scope

How to override base classes' virtual functions that have identical names in multiple inheritance?

烂漫一生 提交于 2019-12-06 04:36:36
问题 Suppose I have two base classes B1 and B2 , and a class D that derives from both B1 and B2 as follows: class B1 { public: // ... virtual void foo() final { cout << "Hello, B1\n"; } }; class B2 { public: // ... virtual void foo() { cout << "Good riddance, B2!\n"; } }; class D :public B1, public B2 { // ... }; In designing the class D , I want to override the member function called foo() from B2; however, foo() in B1 is marked final and prevents me from overriding foo() in B2. What is the best

Why do IUnknown* pointers retrieved through different interfaces of the same COM object have the same value?

人走茶凉 提交于 2019-12-06 03:38:38
I have the following hierarchy of COM interfaces and a class implementing them: interface IX : public IUnknown{}; interface IY : public IUnknown{}; class CA: public IX, public IY{}; Here class CA effectively inherits from IUnknown twice. We know there are two vtable pointers in class CA - one is pointed to IX and another is pointed to IY . So IUnknown stored in the IX subobject is different from IUnknown stored in the IY subobject. Yet when we call IX::QueryInterface() or IY::QueryInterface() on the same object and query IUnknown we get identical IUnknown* pointers. Why does it happen?

compiler's detail of this pointer, virtual function and multiple-inheritance

余生长醉 提交于 2019-12-06 02:58:39
问题 I'm reading Bjarne's paper: Multiple Inheritance for C++. In section 3, page 370, Bjarne said that "The compiler turns a call of a member function into an "ordinary" function call with an "extra" argument; that "extra" argument is a pointer to the object for which the member function is called." I'm confused by the extra this argument. Please see the following two examples: Example 1 :(page 372) class A { int a; virtual void f(int); virtual void g(int); virtual void h(int); }; class B : A

Overriding multiple inherited templated functions with specialized versions

你离开我真会死。 提交于 2019-12-06 02:26:12
问题 Okay, sample code first; this is my attempt at communicating what it is that I'm trying to do, although it doesn't compile: #include <iostream> template <class T> class Base { public: virtual void my_callback() = 0; }; class Derived1 : public Base<int> , public Base<float> { public: void my_callback<int>() { cout << "Int callback for Derived1.\n"; } void my_callback<float>() { cout << "Float callback for Derived\n"; } }; class Derived2 : public Base<int> , public Base<float> { public: void my

Why is this the same even when object pointers differ in multiple inheritance?

二次信任 提交于 2019-12-06 01:58:50
When using multiple inheritance C++ has to maintain several vtables which leads to having "several views" of common base classes. Here's a code snippet: #include "stdafx.h" #include <Windows.h> void dumpPointer( void* pointer ) { __int64 thisPointer = reinterpret_cast<__int64>( pointer ); char buffer[100]; _i64toa( thisPointer, buffer, 10 ); OutputDebugStringA( buffer ); OutputDebugStringA( "\n" ); } class ICommonBase { public: virtual void Common() = 0 {} }; class IDerived1 : public ICommonBase { }; class IDerived2 : public ICommonBase { }; class CClass : public IDerived1, public IDerived2 {

Java: Can a class inherit from two super classes at the same time?

孤人 提交于 2019-12-06 00:04:01
I have a class Journey which I want to make a superclass and another class plannedjourney. The plannedjourney class extends JFrame since it contains forms..However I also want this class to extends Journey.. Is there a possible way to do this? Don't mix models and views. If you keep both domains clearly separated, then you won't be in need of multiple inheritance (this time). You have one model class for journeys and a viewer for such journeys. The viewer should subclass Component and be displayed (somewhere) in your window (a JFrame instance). This viewer takes one journey object and presents

Why the size of empty class that is derived from two empty classes is 2?

折月煮酒 提交于 2019-12-05 23:25:19
What I know is that size of empty class is 1 just to conform to the standard that does not allow objects (and classes thereof) of size to be 0. Here, I am getting size of a derived class D as 2. Why behavior is different in this case given that there is no data member or virtual pointer inherited from class B and C to D? #include<iostream> using namespace std; class A {}; class B : public A {}; class C : public A {}; class D : public B, public C {}; int main() { cout<<"Size of D is: "<<sizeof(D)<<endl; return 0; } To me it seems that whether or not the empty base optimization can be applied

Can I tell which template instance class(es) my class is inheriting from?

江枫思渺然 提交于 2019-12-05 22:29:59
Given this code: template < int I > class Foo { public: int v; Foo() { v = I; } virtual ~Foo() {} }; class Bar : public Foo<0>, public Foo<3> { public: template < int I > int getValue() { return Foo<I>::v; } }; int main() { Bar b; cout << b.getValue<0>() << endl; // prints 0 cout << b.getValue<3>() << endl; // prints 3 cout << b.getValue<4>() << endl; // compiler error return 0; } Is it possible to iterate over all Foo<i> classes from which Bar inherits? We can assume that i is between 0 and some maximum N . In pseudocode: for ( int i = 0; i < N; i++ ) { if ( Bar inherits from `Foo<i>` ) {