Click here to Skip to main content
13,044,428 members (131,504 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 23 Mar 2009

Dynamic Message Passing for C++

, 23 Mar 2009
Rate this:
Please Sign up or sign in to vote.
Dynamic message passing for C++.


Programming languages like Smalltalk and Objective-C are considered more flexible than C++ because they support dynamic message passing. C++ does not support dynamic message passing; it only supports static message passing: when a method of an object is invoked, the target object must have the invoked method; otherwise, the compiler outputs an error.

Although the way C++ does message passing is much faster than the way Smalltalk or Objective-C does it, sometimes the flexibility of Smalltalk or Objective-C is required. This little article shows how it is possible to achieve dynamic message passing in C++ with minimum code.


C++ achieves message passing with the help of vtables.

Smalltalk and Objective C use a method map to achieve the same result.


The supplied header contains an implementation of dynamic message passing. In order to add dynamic message passing capabilities to an object, the following things must be done:

  1. Declare a global function which acts as a signature of the message.
  2. Inherit from dmp::object.
  3. In the class constructor, add one or more messages to the object using the method add_message.
  4. Send a message to an object using the function invoke(signature, ...parameters).

Here is an example:

//the dynamic-message-passing header
#include "dmp.hpp" 

//the message signature
std::string my_message(int a, double b) { return ""; }

//a class that accepts messages dynamically
class test : public dmp::object {
    test() {
        add_message(&::my_message, &test::my_message);

    std::string my_message(int a, double b);

int main() {
    test t;
    std::string s = t.invoke(&::my_message, 10, 3.14);

How it Works

Each object contains a shared ptr to a map. The map's key is the pointer to the signature of the message. The map's value is a pointer to an internal message structure which holds a pointer to the method to invoke.

When a method is invoked, the appropriate message structure is retrieved from the map, and the method of the object stored in the message structure is invoked, using this as the target object.

The map used internally is unordered, for efficiency reasons.

The message map of objects is shared between objects for efficiency reasons as well. When a message map is modified, it is duplicated if it is not unique, i.e., the copy-on-write pattern is applied.

The code is thread safe only when the message maps are not modified while used for look-up by different threads. If a message map is modified, then no thread must send messages to an object at the same time.

The code uses the boost library because of:

  1. unordered maps; the class boost::unordered_map is used.
  2. the pre-processor; used for automatically constructing code for different number of parameters.
  3. shared ptrs; used for internal memory management.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Achilleas Margaritis
Software Developer (Senior)
Greece Greece
No Biography provided

You may also be interested in...

Comments and Discussions

Questionhow can I download the demo code? Pin
jamsbone28-Mar-16 7:01
memberjamsbone28-Mar-16 7:01 
QuestionWell Article is ok Pin
Shakti Misra2-Oct-11 22:06
memberShakti Misra2-Oct-11 22:06 
AnswerRe: Well Article is ok Pin
Achilleas Margaritis4-Oct-11 0:20
memberAchilleas Margaritis4-Oct-11 0:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 23 Mar 2009
Article Copyright 2009 by Achilleas Margaritis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid