Inheritance, virtual interface classes, derived classes

Started by
12 comments, last by Mantear 18 years, 6 months ago
Unfortunately, I do want them to be related. F(x) represents much more than just a simple function. Since I'm using CORBA, everything must be within a class and self contained. If I do something like call another object to perform F, I've just added another set of interfaces. Both Implementaions would require an interface definition that can call that F function, and I'm back where I started.

If you're unfamiliar with CORBA, it's a multi-language multi-platform system that allows you to write a single program over any supported language over any number of computers. You can have one program running on a computer in the U.S. written in C++ call a function that exists on a computer in the U.K. written in Java. A CORBA IDL file defined the interfaces for that function call and generated some C++ and Java headers (my interface/abstract class). As far as the calling C++ routine is concerned, the function is like any other function. CORBA takes care of finding where the function is located, whether it be within the same class, somewhere else on the same PC, over a LAN, or over the Net. A CORBA IDL Interface is translated into a C++ base class with all pure virtual functions. Any time I cross the boundry of the instantiation of a class, I need a new CORBA IDL interface to define that interaction.

In my current situation, I have two CORBA IDLs which call out similar interfaces. I could have a single class which just implements everything, but then I would have a Tx Port which has code/functionallity of an Rx Port as well, which is what I'm trying to avoid.
Advertisement
After reading up on templates some and looking closely at what stylin posted, that's exactly what I needed. However, none of the other examples I saw went so far as to use the template type for a base class. That is indeed very powerful, and seems to open the door for many other possibilities. Are there any pitfalls associated with doing this? Any drawbacks?
Compared to compile-time polymorphism (using templates as described), equivalent run-time polymorphism (inheritance + virtual functions) code is usually slower due to v-table lookups. Compile-time polymorphism adds (sometimes significant amounts) of code bloat as well, although in your specific case this would be negligible.

Also, since we don't have run-time polymorphism available, you can't reference a derived class with a pointer to the base class (what would the template parameter be?), so instead you must simulate it with parameterized functions:
// your CORBA interfaces:class Tx_interface {public:   virtual void F() = 0;           virtual void G() = 0;};class Rx_interface {public:   virtual void F() = 0;           virtual void H() = 0;};#include <iostream>using std::cout;// code squished for brevity ...// templated base classtemplate< typename interface >class Xx_interface : public interface {public:   void F() { cout << "ANYx_object::F()\n"; return; }};// derived classesclass Tx_object : public Xx_interface< Tx_interface > {public:   void G() { cout << "Tx_object::G()\n"; return; }};class Rx_object : public Xx_interface< Rx_interface > {public:   void H() { cout << "Rx_object::H()\n"; return; }};// parameterized function that works with Xx_objectstemplate< class x_object >void some_function( x_object & x_o ) {   x_o.F();   return;}int main() {   // create our x_objects   Tx_object tx_o;   Rx_object rx_o;   // work with our common functionality:   some_function( tx_o );   some_function( rx_o );   return 0;}
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
In my situation, I don't think I'll ever be needing/using a pointer to the base class. If anything, I may get a generic CORBA object ptr (think of them as void*) to a port, which I will then _narrow to either a Tx Port or an Rx Port. My whole reasoning for using a base class was because the two classes were so similar and I didn't want to write the same code twice. It wasn't so I could use polymorphism. I realize that I end up duplicating the code anyways (basically making two base classes, one of each interface type), but at least this way I don't have two pieces of source code which I would need to keep in sync.

Again, thanks for your solution. The results are very clean and exactly what I was looking for. I never would have thought of using a templated class inheritance.

This topic is closed to new replies.

Advertisement