473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Proper interface inheritance

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?
Jul 22 '05 #1
4 1380
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
Jul 22 '05 #2

"Serena" <se*************@ngc.com> wrote in message
news:36**************************@posting.google.c om...
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.
Jul 22 '05 #3
On 14 Jun 2004 11:45:40 -0700, se*************@ngc.com (Serena) wrote:
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 ...
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #4
"Serena" <se*************@ngc.com> wrote in message
news:36**************************@posting.google.c om...
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]

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom Evans | last post by:
My basic question: If I have a specific interface which I know is going to be implemented by a number of classes, but there is no implementation commonality between them, what is the preferred...
4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
4
by: christopher diggins | last post by:
A feature that I find signficantly missing in C# is the ability to write functions in interfaces that can call other functions of the interface. Given an interface ISomeInteface the only way we can...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
7
by: Hazz | last post by:
Are there any good references/articles/books which provide clarity toward my insecurity still on deciding how to model a complex system? I still feel uncomfortable with my understanding, even...
10
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract...
6
by: John Salerno | last post by:
I understand how they work (basically), but I think maybe the examples I'm reading are too elementary to really show their value. Here's one from Programming C#: #region Using directives ...
4
by: Raja Chandrasekaran | last post by:
Hai friends, I really wonder, If the interface does not have any definition, Y do we need to use interface. You can then only we can use Multiple inheritance. I really cant understand, Just for...
1
by: mattmao | last post by:
I am brand new to C#.NET so here is my trial on this lab exercise: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace lab02exec { ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.