locked
How to clone an object? RRS feed

  • Question

  • I define two classes in C++, as follows:

     

    class CBaseClass

    {

    }

     

    class CDerivedClass : public CBaseClass

    {

    }

     

    And want to implement a clone function as follows:

     

    CBaseClass *Clone(const CBaseClass *pObject)

    {

    }

     

    When an object of CDerivedClass is passed to Clone, then the function will also create a CDerivedClass object and return.

    When an object of CBaseClass is passed to Clone, then the function will also create a CBaseClass object and return.

     

    How to implement such a feature?

    Thursday, July 24, 2014 4:46 PM

Answers

  • Typically, you would implement Clone as a virtual method of the base class, overridden in each derived class.

    Igor Tandetnik

    Thursday, July 24, 2014 6:53 PM