I'm reading C++ Design Patterns and Derivatives Pricing by Mark Joshi and implementing his code in C++11. Everything has gone pretty well until I hit chapter 4 where he discusses virtual copy constructors.
PayOffDoubleDigital thePayOff(Low, Up);
VanillaOption theOption(thePayOff, Expiry);
The problem here is that VanillaOption
contains a reference to thePayOff
. If that's the case and someone modifies thePayOff
, the the behavior of theOption
could be modified unwittingly. The solution he advises is to create a virtual copy constructor in PayOffDoubleDigital
's base class, PayOff
so that theOption
contains its own copy:
virtual PayOff* clone() const = 0;
and then defined in each inherited class:
PayOff* PayOffCall::clone() const
{
return new PayOffCall(*this);
}
Returning new caught me as something that might be inappropriate in C++11. So what is the proper way of handling this using C++11?