I have a working Cloneable/CloneableImpl pair below. It does its job as long as I have default constructors from child to parent.
Suppose that the Animal constructor is modified to Animal( std::string const& name ) , which requires a name that must be passed from the constructors of the children classes.
How can I include this requirement in a structure while retaining the Cloneable/CloneableImpl generic?
In other words, I need to be able to forward all constructor arguments from Lion, Tiger to Animal. Is there a way to do this in C ++ 11 in a generic way?
If this is not possible, how can these templates be restructured to remain generally accepted, given the requirement of the constructor?
the code
template<typename P> struct Cloneable { virtual P* clone() const = 0; }; template<typename T,typename P> struct CloneableImpl : public P { virtual P* clone() const { return new T( dynamic_cast<T const&>(*this)); } };
c ++ constructor clone c ++ 11 templates
kfmfe04
source share