Click here to Skip to main content
Licence 
First Posted 27 Nov 2004
Views 28,828
Bookmarked 21 times

Polymorphism without Planning

By Christopher Diggins | 27 Nov 2004
The OOTL uses a technique of allowing run-time polymorphism on objects, without requiring them to plan for it.
4 votes, 28.6%
1
2 votes, 14.3%
2

3
1 vote, 7.1%
4
7 votes, 50.0%
5
3.46/5 - 14 votes
μ 3.46, σa 3.26 [?]

Making Objects Polymorphic without Intrusion

It is generally assumed that in C++, in order to achieve run-time polymorphic behavior, 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 requiring 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.

You can download the latest OOTL through SourceForge here but you will need the latest Boost library version 1.32.0 in order to compile it, which you can also get through SourceForge here.

Given two existing classes 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 on C++, 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Christopher Diggins

Architect
Autodesk
Canada Canada

Member

Follow on Twitter Follow on Twitter
I've been programming personal computers since my I got my first Atari 400 in 1980. I currently work at Autodesk as an SDK specialist.
 
One of my passions is the design and implementation of programming languages. My current project is the Jigsaw library which includes a parsing engine and other utilities for implementing programming languages in C#.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questiondynamic_any anyone ? Pinmemberalnicodon1:06 11 Apr '08  
Generallatent typing... PinmemberRüdiger Klaehn15:26 27 Nov '04  
GeneralInteresting... but... Pinmemberronhash13:23 27 Nov '04  
GeneralRe: Interesting... but... Pinmembercdiggins15:22 27 Nov '04  
Generalcool! PinmemberHugo Hallman10:09 27 Nov '04  
GeneralRe: cool! PinmemberNeville Franks10:23 27 Nov '04  
GeneralRe: cool! Pinmembercdiggins15:14 27 Nov '04  
GeneralRe: cool! Pinmembercdiggins8:41 28 Nov '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120206.1 | Last Updated 27 Nov 2004
Article Copyright 2004 by Christopher Diggins
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid