Proper interface inheritance

S

Serena

I am attempting to complete a program with the following format:

Interface2 inherits from Interface1. Both of these are virtual
classes.

Implementation1 inherits from Interface1. Implementation2 inherits
from Interface2 and Implementation1.

When using Implementation2 and a call is made on an operation that is
inherited from Implementation1, the code is entered for Interface1
rather than Implementation1.

The environment is C++ with CORBA (although these particular classes
are not CORBA interfaces) in the Green Hills Integrity operating
system.

How do I make the code in Implementation1 get executed when called on
Implementation2?
 
V

Victor Bazarov

Serena said:
I am attempting to complete a program with the following format:

Interface2 inherits from Interface1. Both of these are virtual
classes.

There is no such thing as "virtual class" in C++. The only thing
that comes close is a class template (that is not a class until
you specify its arguments).

Perhaps you meant "abstract class". I will go with that for now.
Implementation1 inherits from Interface1. Implementation2 inherits
from Interface2 and Implementation1.

When using Implementation2 and a call is made on an operation that is
inherited from Implementation1, the code is entered for Interface1
rather than Implementation1.

What do you mean?
The environment is C++ with CORBA (although these particular classes
are not CORBA interfaces) in the Green Hills Integrity operating
system.

How do I make the code in Implementation1 get executed when called on
Implementation2?

struct Interface1 { virtual void foo() = 0; };
struct Interface2 { virtual void bar() = 0; };

struct Impl1 : Interface1 { void foo(); };
struct Impl2 : Interface2, Impl1 { void bar(); };

#include <iostream>
void Interface1::foo() { std::cout << "Interface1::foo\n"; }
void Interface2::bar() { std::cout << "Interface2::bar\n"; }
void Impl1::foo() { std::cout << "Impl1::foo\n"; }
void Impl2::bar() { std::cout << "Impl2::bar\n"; }

int main() {
Impl2 i2;
i2.foo(); // inherited
i2.bar(); // its own
Interface1& ii1 = i2;
ii1.foo(); // polymorphic call to inherited
Interface2& ii2 = i2;
ii2.bar(); // polymorphic call to own
}

--------------------------- prints
Impl1::foo
Impl2::bar
Impl1::foo
Impl2::bar
 
J

jeffc

Serena said:
I am attempting to complete a program with the following format:

Interface2 inherits from Interface1. Both of these are virtual
classes.

Implementation1 inherits from Interface1. Implementation2 inherits
from Interface2 and Implementation1.

When using Implementation2 and a call is made on an operation that is
inherited from Implementation1, the code is entered for Interface1
rather than Implementation1.

You're going to have to show some code. Simplify as much as possible so
it's compilable but still shows the problem. Could be lack of virtual
functions, or any number of things.
 
B

Bob Hairgrove

I am attempting to complete a program with the following format:

Interface2 inherits from Interface1. Both of these are virtual
classes.

Implementation1 inherits from Interface1. Implementation2 inherits
from Interface2 and Implementation1.

When using Implementation2 and a call is made on an operation that is
inherited from Implementation1, the code is entered for Interface1
rather than Implementation1.

The environment is C++ with CORBA (although these particular classes
are not CORBA interfaces) in the Green Hills Integrity operating
system.

How do I make the code in Implementation1 get executed when called on
Implementation2?

Sounds pretty much like a homework assignment to me ...
 
C

Cy Edmunds

Serena said:
I am attempting to complete a program with the following format:

Interface2 inherits from Interface1. Both of these are virtual
classes.

Assuming you mean "abstract classes" this is generally a bad idea. I advise
you to start each abstract class from scratch and use multiple inheritance
to create an object which exports both interfaces.
Implementation1 inherits from Interface1. Implementation2 inherits
from Interface2 and Implementation1.

That's why it's a bad idea. Now Implementation2 has two inheritance paths
starting from Interface1. (I was going to show the inheritance diagram but I
know what my newsreader would do to my ascii art. 8P) The point is that
Implementation2 inherits from Interface2 which inherits from Interface1. It
also inherits from Implementation1 which again inherits from Interface1.
This pattern, sometimes called the "diamond of death", can be dealt with
using virtual inheritance but it is a pain which can generally be avoided.

[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top