I have a requirement of a templated clone() method which Base classes can implement through an ICloneable interface and all the derived classes will automatically get a pure virtual clone<T>() method to override. I know this is confusing, so below is my code which probably explains it better.// ICloneable interfacetemplate <class T>class ICloneable{public:    // smart pointer based polymorphic clone method     // of template parameter T    virtual std::unique_ptr<T> clone() const = 0;};// Any class deriving from ICar would need to // implement std::unique_ptr<ICar> clone()// and std::string name()class ICar : public ICloneable<ICar>{public:    virtual std::string name() = 0;    virtual ~ICar() {}};// Derived car object implementing ICar interfaceclass NamedCar : public ICar{private:    std::string _name;public:    NamedCar() {_name = This car does not have a name;}    NamedCar(std::string name) { _name = name; }    virtual ~NamedCar() {}    // override    virtual std::string name() { return _name; }    // override    virtual std::unique_ptr<ICar> clone() const    {        return std::unique_ptr<ICar>(new NamedCar(*this));    }};class MercCar : public ICar{public:    MercCar() {}    virtual ~MercCar() {}    // override    virtual std::string name() { return Merc; }    // override    virtual std::unique_ptr<ICar> clone() const     {        return std::unique_ptr<ICar>(new MercCar(*this));    }};// Any class deriving from IBicycle would need to // implement std::unique_ptr<IBicycle> clone()// and void describe()class IBicycle : public ICloneable<IBicycle>{public:    virtual ~IBicycle() {}    virtual void describe() =  0;};class MyBicycle : public IBicycle{public:    MyBicycle() {}    virtual void describe()     {        std::cout << This is my bicycle << std::endl;    }    virtual std::unique_ptr<IBicycle> clone() const    {        return std::unique_ptr<IBicycle>(new MyBicycle(*this));    }    virtual ~MyBicycle() {} };template <typename T>class MyGarage{private:    T _data;    std::unique_ptr<ICar> _car_ptr;    std::unique_ptr<IBicycle> _bicycle_ptr;public:    MyGarage(T& data, const std::unique_ptr<ICar>& cp, std::unique_ptr<IBicycle>& bp) : _data(data)    {        _car_ptr = cp->clone();        _bicycle_ptr = bp->clone();    }    MyGarage(const MyGarage& source) : data(source._data)    {        _car_ptr = source._car_ptr->clone();        _bicycle_ptr = source._bicycle_ptr->clone();    }    MyGarage& operator=(const MyGarage& source)    {        if (this != &source)        {            _data = source.data;            _car_ptr = source._car_ptr->clone();            _bicycle_ptr = source._bicycle_ptr->clone();        }    return *this;    }    std::unique_ptr<ICar> car_ptr()    {        return _car_ptr->clone();    }    std::unique_ptr<IBicycle> bicycle_ptr()    {        return _bicycle_ptr->clone();    }    void car_ptr(const std::unique_ptr<ICar>& cp)    {        _car_ptr = cp->clone();    }    void bicycle_ptr(const std::unique_ptr<IBicycle> bp)    {        _bicycle_ptr = bp->clone();    }    T data()    {        return _data;    }    void data(const T& data)    {        _data = data;    }};int main(){    // Check whether the clone method works for IBicycle and ICar    std::unique_ptr<IBicycle> p_bike;    MyBicycle mybike;    p_bike = mybike.clone();    p_bike->describe();    std::unique_ptr<ICar> p_car;    NamedCar n_car(Ford);    NamedCar kia_car(Kia);    MercCar my_car;    p_car = n_car.clone();    std::cout << p_car->name() << std::endl;    p_car = kia_car.clone();    std::cout << p_car->name() << std::endl;    p_car = my_car.clone();    std::cout << p_car->name() << std::endl;    p_car = n_car.clone();    std::cout << p_car->name() << std::endl;    NamedCar unnamed_car;    p_car = unnamed_car.clone();    std::cout << p_car->name() << std::endl;    int x = 123;    // Check the functionality of MyGarage class    MyGarage<int> garage(x, p_car, p_bike);    std::cout << garage.car_ptr()->name() << std::endl;    garage.bicycle_ptr()->describe();    garage.car_ptr(n_car.clone());    std::cout << garage.car_ptr()->name() << std::endl;    garage.car_ptr(my_car.clone());    std::cout << garage.car_ptr()->name() << std::endl;    int newx = x + 982;    std::cout << garage.data() << std::endl;    garage.data(newx);    std::cout << garage.data() << std::endl;}The output is:This is my bicycleFordKiaMercFordThis car does not have a nameThis car does not have a nameThis is my bicycleFordMerc1231105which I expected.However, I wanted somebody's feedback on this i.e. whether this is a good design for such a problem, especially the MyGarage class - where I have to expose a setter for the internal unique_ptr, which I am not sure is a good idea. Also, is there a possibility of this design unknowingly leaking memory?
Polymorphic template cloning class
c++;design patterns;interface;pointers