Professional C__ - Marc Gregoire [475]
template class SimpleTemplate { public: SimpleTemplate(T& inObject); const T& get() const; void set(T& inObject); protected: T& mObject; }; #include "SimpleTemplate.cpp" // Include the implementation! Code snippet from Template\SimpleTemplate.h template SimpleTemplate { } template const T& SimpleTemplate { return mObject; } template void SimpleTemplate { mObject = inObject; } Code snippet from Template\SimpleTemplate.cpp #include #include #include "SimpleTemplate.h" using namespace std; int main() { // Try wrapping an integer. int i = 7; SimpleTemplate i = 2; cout << "wrapper value is " << intWrapper.get() << endl; // Try wrapping a string. string str = "test"; SimpleTemplate str += "!"; cout << "wrapper value is " << stringWrapper.get() << endl; return 0; } Code snippet from Template\TemplateTest.cpp Details about templates can be found in Chapters 19 and 20. THERE MUST BE A BETTER WAY As a Professional C++ programmer, you need to spend less of your time reinventing the wheel, and more of your time adapting reusable concepts in new ways. The following techniques are general-purpose approaches that you can apply directly to your own programs or customize for your needs. Double Dispatch Double dispatch is a technique that adds an extra dimension to the concept of polymorphism. As described in Chapter 3, polymorphism lets the program determine behavior based on run-time types. For example, you could have an Animal class with a move() method. All Animals move, but they differ in terms of how they move. The move() method is defined for every subclass of Animal so that the appropriate method can be called, or dispatched, for the appropriate animal at run time without knowing the type of the animal at compile time. Chapter 8 explains how to use virtual methods to implement this run-time polymorphism. Sometimes, however, you need a method to behave according to the run-time type of two objects, instead of just one. For example, suppose that you want to add a method to the Animal class that returns true if the animal eats another animal and false otherwise. The decision is based on two factors — the type of animal doing the eating, and the type of animal being eaten. Unfortunately, C++ provides no language mechanism to choose a behavior based on the run-time type of more than one object. Virtual methods alone are insufficient for modeling this scenario because they determine a method, or behavior, depending on the run-time type of only the receiving object. Some object-oriented languages provide the ability to choose a method at run time based on the run-time types of two or more objects. They call this feature multi-methods. In C++, however, there is no core language feature to support multi-methods, but you can use the double dispatch technique, which provides a technique to make functions virtual for more than one object. Double dispatch is really a special case of multiple dispatch, in which a behavior is chosen depending on the run-time
As you read this paragraph, thousands of C++ programmers throughout the world are solving problems that have already been solved. Someone in a cubicle in San Jose is writing a smart pointer implementation from scratch that uses reference counting. A young programmer on a Mediterranean island is designing a class hierarchy that could benefit immensely from the use of mix-in classes.