Serena wrote:
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
-----------------------------------
How is that not what you want?
Victor