Sponsored Link •
|
Summary
It is commonly assumed that in order to achieve some measure of run-time polymorphism we have to state in a class declaration that it it either implements an interface or it inherits from a base class with virtual functions. Not anymore ...
Advertisement
|
It is generally assumed that In C++ in order to achieve run-time polymorphic behaviour, we have to inherit from a base class with virtual functions. Java, and other languages also provide us with interfaces, but the classes have to explicitly state their intention to implement the interface.
This is a misconception that a lot of people understandably have when they first hear about the C++ Object Oriented Template Library ( OOTL ). In the September 2004 issue of the C/C++ Users Journal I outline a technique in C++ for implementing interface reference types without requrining changes to existing objects. This technique is used in the upcoming Boost Interfaces Library (BIL) by Jonathan Turkanis. The BIL is not yet ready for a public beta release, but a pre-release version has been included with the OOTL version 0.1. Given two existing class such as:class Faz { public: int Fu() { return 1; } void Bar(int x) { printf("faz %d\n", x); } } class Baz { public: int Fu() { return 2; } void Bar(int x) { printf("baz %d\n", x); } }The BIL can then be used to express an interface after these classes have been defined elsewhere as follows:
BOOST_IDL_BEGIN(IFuBar) BOOST_IDL_FN0(Fu, int) BOOST_IDL_FN1(Bar, void, (int, x)) BOOST_IDL_END(IFuBar)You will have to put the blame for the ugly syntax squarely on the shoulders of the C++ design committee, not Jonathan. C++0x will surely enable improved syntax, especially if Jonathan or myself have anything to say about it ( in fact I would like to see interface references gain first class status ). The BOOST_ prefix, is a requirement for consideration in Boost, which clearly is the eventual goal of the BIL. Once we define this interface, after the fact, we can use it to make Faz and Baz, polymorphic. Consider the following:
int main{ faz f; baz b; IFuBar i; if ((rnd() % 2) == 0) { i = f; } else { i = b; } i.Bar(i.Fu()); }This code snippet prints out either faz 1 or baz 2. And that's all I have to say about that.
Have an opinion? Readers have already posted 21 comments about this weblog entry. Why not add yours?
If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.
Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com. |
Sponsored Links
|