Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I wrote a Stack and Queue implementation (Linked List based). There is one stack (bigStack). For example, I separate bigStack (example: stackA and stackB). I pop() a node from bigStack, I push() in stackA. In the same way, I push() in stackB. I want bigStack to not change. Therefore I want to clone the bigStack object. How do I clone objects in C++? Or is there another solution to my problem?

class Stack : public List { public:    Stack() {}    Stack(const Stack& rhs) {}    Stack& operator=(const Stack& rhs) {};     ~Stack() {}      int Top() {         if (head == NULL) {             cout << "Error: The stack is empty." << endl;             return -1;         } else {             return head->nosu;         }     }      void Push(int nosu, string adi, string soyadi, string bolumu) {         InsertNode(0, nosu, adi, soyadi, bolumu);     }      int Pop() {         if (head == NULL) {             cout << "Error: The stack is empty." << endl;             return -1;         } else {             int val = head->nosu;             DeleteNode(val);             return val;         }     }      void DisplayStack(void);  }; 

then...

Stack copyStack = veriYapilariDersi; copyStack.DisplayStack(); 
like image 803
mert Avatar asked Oct 15 '12 19:10

mert


People also ask

What is clone () method in C?

C# | Clone() Method In C#, Clone() is a String method. It is used to clone the string object, which returns another copy of that data. In other words, it returns a reference to this instance of String. The return value will be only another view of the same data. Clone method called directly on current String instance.

Can objects be cloned?

The quantum uncertainty principle is one such law that makes cloning of extremely tiny objects, like electrons, impossible. Bigger objects like books, however, follow classical laws and can be copied. Physicists call cloning of these larger objects as 'classical cloning'.


1 Answers

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; }; 
like image 189
John Dibling Avatar answered Sep 23 '22 07:09

John Dibling