Polymorphic Copy Construction
When using polymorphism, one quite often needs to create copies of objects by the base class pointer. A commonly used idiom for this is adding a virtual clone function that is defined in every derived class. The CRTP pattern can be used to avoid having to duplicate that function or other similar functions in every derived class.
// Base class has a pure virtual function for cloning class Shape { public: virtual ~Shape {} virtual Shape *clone const = 0; }; // This CRTP class implements clone for Derived templateThis allows obtaining copies of squares, circles or any other shapes by shapePtr->clone.
Read more about this topic: Curiously Recurring Template Pattern
Famous quotes containing the words copy and/or construction:
“Quotationsalways inexact. I dont trust people who cannot even copy out.”
—Jean Rostand (18941977)
“No real vital character in fiction is altogether a conscious construction of the author. On the contrary, it may be a sort of parasitic growth upon the authors personality, developing by internal necessity as much as by external addition.”
—T.S. (Thomas Stearns)