How to clone object in C++ ? Or Is there another solution?

前端 未结 3 1973
南方客
南方客 2020-12-01 03:48

I wrote a Stack and Queue implementation (Linked List based). There is one stack (bigStack). For example, I separate bigStack (example: stack

相关标签:
3条回答
  • 2020-12-01 04:15

    The typical solution to this is to write your own function to clone an object. If you are able to provide copy constructors and copy assignement operators, this may be as far as you need to go.

    class Foo
    { 
    public:
      Foo();
      Foo(const Foo& rhs) { /* copy construction from rhs*/ }
      Foo& operator=(const Foo& rhs) {};
    };
    
    // ...
    
    Foo orig;
    Foo copy = orig;  // clones orig if implemented correctly
    

    Sometimes it is beneficial to provide an explicit clone() method, especially for polymorphic classes.

    class Interface
    {
    public:
      virtual Interface* clone() const = 0;
    };
    
    class Foo : public Interface
    {
    public:
      Interface* clone() const { return new Foo(*this); }
    };
    
    class Bar : public Interface
    {
    public:
      Interface* clone() const { return new Bar(*this); }
    };
    
    
    Interface* my_foo = /* somehow construct either a Foo or a Bar */;
    Interface* copy = my_foo->clone();
    

    EDIT: Since Stack has no member variables, there's nothing to do in the copy constructor or copy assignment operator to initialize Stack's members from the so-called "right hand side" (rhs). However, you still need to ensure that any base classes are given the opportunity to initialize their members.

    You do this by calling the base class:

    Stack(const Stack& rhs) 
    : List(rhs)  // calls copy ctor of List class
    {
    }
    
    Stack& operator=(const Stack& rhs) 
    {
      List::operator=(rhs);
      return * this;
    };
    
    0 讨论(0)
  • 2020-12-01 04:17

    In C++ copying the object means cloning. There is no any special cloning in the language.

    As the standard suggests, after copying you should have 2 identical copies of the same object.

    There are 2 types of copying: copy constructor when you create object on a non initialized space and copy operator where you need to release the old state of the object (that is expected to be valid) before setting the new state.

    0 讨论(0)
  • 2020-12-01 04:22

    If your object is not polymorphic (and a stack implementation likely isn't), then as per other answers here, what you want is the copy constructor. Please note that there are differences between copy construction and assignment in C++; if you want both behaviors (and the default versions don't fit your needs), you'll have to implement both functions.

    If your object is polymorphic, then slicing can be an issue and you might need to jump through some extra hoops to do proper copying. Sometimes people use as virtual method called clone() as a helper for polymorphic copying.

    Finally, note that getting copying and assignment right, if you need to replace the default versions, is actually quite difficult. It is usually better to set up your objects (via RAII) in such a way that the default versions of copy/assign do what you want them to do. I highly recommend you look at Meyer's Effective C++, especially at items 10,11,12.

    0 讨论(0)
提交回复
热议问题