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();
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.
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'.
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; };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With