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:
“Learn hence for ancient rules a just esteem;
To copy Nature is to copy them.”
—Alexander Pope (16881744)
“There is, I think, no point in the philosophy of progressive education which is sounder than its emphasis upon the importance of the participation of the learner in the formation of the purposes which direct his activities in the learning process, just as there is no defect in traditional education greater than its failure to secure the active cooperation of the pupil in construction of the purposes involved in his studying.”
—John Dewey (18591952)