Also known as delegated polymorphism.
Sometimes, one requires more dynamic behaviour than that conveniently offered by C++. Those who wish to enjor the performance and speed advantages of C++ and yet desire the flexibility of more dynamic languages can achieve it with a bit of work.
The class outlined below has a lot of characteristics similar to those of more dynamic languages. They allow objects to change their "type" at runtime, they allow objects to decide at runtime whether or not they "know what to do" with a particular request for a method invocation. This kind of framework is especially suited to classes that are loaded at run time. That's not a typo. I didn't mean "objects". You can indeed modify a program while it's running!
The following is an example of what James Coplien calls the Envelope and Letter Idiom. The class Envelope acts as a "bucket" in which to store other objects, namely "letters". Clients interact with the Envelope. This has some advantages:
If we want to avoid enormous numbers of stubbs like this one:
return Rep -> someMethod();in Envelope, we can do so by overloading operator-> as follows:
Letter* operator->(){ return Rep; }Then we can invoke methods on envelopes using the arrow syntax:
myEnvelope->method1();
Once you're done with that, make sure you've read about registration, and come see a more in depth example.