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 111457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:23:18+00:00 2026-05-11T02:23:18+00:00

Alright, so I’m trying out C++ for the first time, as it looks like

  • 0

Alright, so I’m trying out C++ for the first time, as it looks like I’ll have to use it for an upcoming course in college. I have a couple years of programming under my belt, but not much in the non-garbage-collected world.

I have a class, a Node for use in a doubly linked list. So basically it has a value and two pointers to other Nodes. The main constructor looks like Node(const std::string & val, Node * prev, Node * next). The exercise includes a copy constructor that does a shallow copy of another Node, with a comment above it that says to change it to make a deep copy.

Here is what I thought that meant:

Node(const Node & other)       : value(other.value) {   prev = new Node(other.prev->value, other.prev->prev, other.prev->next);   next = new Node(other.next->value, other.next->prev, other.next->next); } 

This seems to accomplish the goal of making it so that changing the copied Node doesn’t affect the new Node. However, when I do it this way, I am allocating new stuff on the heap. This worries me, because I think it means that I should also be deleting it in the Node’s destructor. But this is now inconsistent with the other constructor, where pointers to the Nodes are just passed in, already pointing to something. I can’t rightly go deleteing next and prev in the destructor with that going on, right?

I’m really confused, guidance appreciated!

EDIT: Here is the code (before my above change to it), as requested:

#include <string>  //! Node implements a doubly-linked list node class Node {     friend class LinkedList;  //!< LinkedList can access private members of Node public:      //!  Constructor     Node(const std::string & v, Node * p, Node * n) :       value(v), prev(p), next(n)     {     }      //! Change to deep copy     Node(const Node & other) :       value(other.value), prev(other.prev), next(other.next)     {     }      //!  Read-only public methods for use by clients of the LinkedList class     const std::string & GetValue() const     {       return value;     }       Node * GetPrevious()const     {       return prev;     }       Node * GetNext()const     {       return next;     }      //! Change to deep copy     Node & operator=(const Node & other)     {         if(this!=&other)         {             value=other.value;             prev=other.prev;             next=other.next;         }         return *this;     }   private:     std::string value;        //!< value stored in the node     Node * prev;            //!< pointer to previous node in the list     Node * next;            //!< pointer to next node in the list }; 
  • 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. 2026-05-11T02:23:19+00:00Added an answer on May 11, 2026 at 2:23 am

    First of all, I’m not really sure how the objective of the exercise should be understood. How deep should the copy be? In a solution like yours, this->next->next and other.next->next would be still the same thing. Should this object also be duplicated? And the rest of the list? Where does it end? One could of course deep-copy the whole list, but this would be a quite unexpected behavior of a copy constructor of a single node, I think.

    Is maybe the value member variable a pointer, that is supposed to be deep copied? That would make much more sense for me.

    But back to your interpretation:

    Node a(...); // ... more code that adds a whole list to a Node b(a); 

    There are two problems with your implementation. For one b->next->prev points to a, while I suspect it should point back to b. Secondly you need to think about the corner cases, where a might be the first or last node in the list.

    And to your main question: you are of course right, somewhere the newly created objects need to be deleted again. No matter if you just copy the prev and next nodes or the whole list, I would say the user of that copy is responsible to delete all the copied nodes again. I assume with a normal, not-copied list, the user of that list would walk through all the nodes and delete them manually one after another, once he’s done with the list. He wouldn’t not assume the destructor of one node to delete the whole list. And the same goes for copies, they should behave the same. The user of the copied stuff should delete all the copies. (In practice you would probably have a list class, that does all that node management for you).

    But again, if the copy constructor of the node copies the whole list, or even just several of it’s nodes, this would be very unexpected and all the time people would forget to clean up all these copies. But that’s not your node class’ fault, but the exercise requirements’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Well, because you set your Maximum to 10, your progress… May 11, 2026 at 1:55 pm
  • added an answer Just use: bar(x, y, z); X, Y, and Z are… May 11, 2026 at 1:55 pm
  • added an answer Make a common form and pass it a delegate pointing… May 11, 2026 at 1:55 pm

Related Questions

Alright. So I figure it's about time I get into unit testing, since everyone's
Alright. So I have a very large amount of binary data (let's say, 10GB)
Alright, so I have a query that looks like this: SELECT `orders`.*, GROUP_CONCAT( CONCAT(
Alright, so I'm trying out C++ for the first time, as it looks like
Alright so I have this C++ image capturing class. I was wondering if I
Alright, so I'm working on programming my own installer in C#, and what I'd

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.