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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:24:26+00:00 2026-05-20T05:24:26+00:00

This is a follow up to a question I asked earlier today regarding the

  • 0

This is a follow up to a question I asked earlier today regarding the appropriation of directly calling a class’ destructor.

I’m creating my own List for an assignment. I have overloaded the assignment, square brackets, in and out stream operators and have the basic adding, removing and printing to screen functionality.

My next step is to have the List delete and release the memory of all of its nodes, which I am having trouble with.

I have the List write to the screen its current state after performing its operations, as follows:

void main()
{
    // Create instance of list
    List aList(true);
    List bList(true);
    aList.Add(1);
    aList.Add(2);
    bList.Add(2);
    cout << "aList: " << aList;
    cout << "bList: " << bList;
    aList = bList;
    cout << "aList: " << aList;
    cout << "bList: " << bList;
    aList.Add(3);
    cout << "aList: " << aList;
    cout << "bList: " << bList;
    int i = 0; cin >> i;
}

Here is my overloaded out stream:

ostream &operator<<(ostream &stream, List theList)
{
    stream << "There are " << theList._totalNodes << " nodes in the list." << endl;
    for(int i = 0; i < theList._totalNodes; i++)
    {
        stream << "Node #" << i << " holds the data: " << theList[i] << endl;
    }
    return stream;
}

Looking at the command prompt window after this is used “Destructor called” is also printed underneath it.

The List destructor:

List::~List()
{
    cout << "Destructor called" << endl;
//  List::Destroy();
}

The overloaded assignment operator also seems to call the destructor, which doesn’t surprise me. However, the others do.

List &List::operator=(List &aList)
{
    // Make sure not the same object trying to be copied
    if(this != &aList)
    {
        // Perform deep copy
        // Delete old pointers in redundant list, only if they exist already
        if(_head)
        {
            delete _head, _tail, _current, _del;
            _totalNodes = 0; // Reset total nodes
        }
        // Now set new pointers copied from other list
        _head = NULL;
        _tail = _head;
        _current = _head;
        _del = _head;
        // Now loop through all nodes in other list and copy them to this
        // Reset pointer in other list
        aList.Reset();
        for(int i = 0; i < aList._totalNodes; i++)
        {
            Add(aList.Current()->_data);
            aList.Next();
        }
        // Reset other list's pointer
        aList.Reset();
        Reset(); // Reset our pointer
    }
    return *this; // Return for multi assignment
}

I also have the destructor to a List write “Destructor called” when it’s called and noticed that it is called after every use of one of its methods.

Why is this? I assumed the destructor is called when the object is no longer needed, i.e. deleted.

Furthermore, when stepping through my code I have noticed that when a pointer is deleted the memory address it is pointing to isn’t being nullified. Do I have to manually NULL a pointer after it is deleted?

  • 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-05-20T05:24:27+00:00Added an answer on May 20, 2026 at 5:24 am

    It’s because you took your List by value- that is, you copied it. You need to take it by const reference- that is,

    ostream &operator<<(ostream &stream, const List& theList)

    Furthermore, your assignment operator should take a const reference, not a non-const ref.

    I also don’t think that this

    delete _head, _tail, _current, _del;

    does what you think it does. It just deletes _del and none of the others.

    You should use a recursively self-owning design, where each node deletes the next one in the chain. Edit: As this was unclear, I’m going to post a brief sample.

    template<typename T> struct Node {
        Node(const T& arg, Node* prev) : t(arg), previous(prev) {}
        T t;
        std::auto_ptr<Node<T>> next;
        Node<T>* previous;
    };
    template<typename T> class List {
        std::auto_ptr<Node<T>> head;
        Node<T>* tail;
    public:
        List() : head(NULL), tail(NULL) {}
        void clear() { head = tail = NULL; } // Notice how simple this is
        void push_back() {
            push_back(T());
        }
        void push_back(const T& t) {
            if (tail) {
                tail = (tail->next = new Node<T>(t, tail)).get();
                return;
            }
            tail = (head = new Node<T>(t, NULL)).get();
        }
        void pop_back() {
            tail = tail->previous;
            tail->next = NULL;
        }
        void pop_front() {
            head = head->next;
        }
        List<T>& operator=(const List& ref) {
            clear(); // Clear the existing list
            // Copy. Gonna leave this part to you.
        }
    };
    

    Notice how I never deleted anything- std::auto_ptr did everything for me, and manipulating the list and it’s nodes became much easier, because std::auto_ptr manages all of the memory involved. This list doesn’t even need a custom destructor.

    I suggest that you read up heavily on std::auto_ptr, it has some surprises in it’s interface, like the “copy” constructor and assignment operator actually move, that you are going to want to know about before using the class. I wish I could start using std::unique_ptr instead all of the time, which would make life so much easier.

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

Sidebar

Related Questions

earlier today I asked this question . So since moq creates it's own class
This is a follow-up to another question I asked earlier today. I am creating
This is a follow-up question to this question I asked earlier. Btw thanks Neil
This is a follow up to a question I asked earlier seen here: Confused
This is a follow-up to a question I'd asked earlier which phrased this as
This is a follow up to this question I asked earlier: Why can't I
This is a follow up to the question I asked earlier. Basically I have
This is a follow up question to a previous question I asked about calculating
This is a follow-on question from the one I asked here . Can constraints
This question is kind of a follow up to this question I asked a

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.