Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:00:07+00:00 2026-06-12T23:00:07+00:00

I wrote a Stack and Queue implementation (Linked List based). There is one stack

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:00:08+00:00Added an answer on June 12, 2026 at 11:00 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a method which creates a copy of linked list. Can you guys
I wrote an implementation for a priority queue I needed, and now I would
I had to write a linked list, then turn it into a dynamic Stack,
I recently wrote a node-based stack class, per instructions (specs in the comments before
My colleague wrote the following stackoverflow question: other stack overflow question on this topic
i have a question regarding a stack overflow in C. I wrote a little
I wrote 3 functions that count the number of times an-element appears in a-list.
Design a data representation mapping a stack S and a queue Q into a
I wrote a C program in which I did some pretty heavy stack allocation,
Using the advice of another stack overflow question I wrote my own LogWriter class:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.