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

  • Home
  • SEARCH
  • 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 3333370
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:47:21+00:00 2026-05-17T23:47:21+00:00

Hi I have some issue regarding constructor and destructor. I have list class, which

  • 0

Hi
I have some issue regarding constructor and destructor. I have list class, which has two inner classes, one private class for the list nodes, and one public iterator class.

Now for the issue, I have written a non-member print function which uses the inner iterator class. When i use this non-member function it will end calling the destructor for the iterator. It doesn’t end here though because for some reason it will also call for the list class’s destructor. Which causes some problem when I want to print the list content again.

I don’t understand why it call the list class destructor as well and wonder if someone kindly can tell me that, and how I should fix it.

I have attached all the code related to the problem

Main

#include <iostream>

#include "sorted_list.h"
#include "iterator.h"
using namespace std;

void list_print(ostream& os, sorted_list list)
{
    sorted_list::iteratorn it(&list);

    while( ! it.iterator_end())
    {
        os <<   "key = " << setw(3) << it.iterator_get_key()   << ", "
        << "value = " << setw(5) << it.iterator_get_value() << endl;
        it.iterator_next();
    }

    os << endl;
}

int main()
{


    sorted_list a;
    a.insert(4,4);
    a.insert(5,5);


    list_print(cout,a);
    list_print(cout,a);
}

sorted_list.cc

#include "sorted_list.h"

sorted_list::sorted_list()
{
    cout << "construct sorted_list" << endl;
    this->first = 0;
}
sorted_list::~sorted_list()
{
    cout << "destruct sorted_list" << endl;
    destroy(this->first);

}

void sorted_list::destroy(list_link* item)
{
    cout << "destroy list_link" << endl;
    if(item)
    {
        destroy(item->next);
        delete item;
    }

}

void sorted_list::insert(int key, double value)
{
    list_link *curr;
    list_link *prev = 0;

    curr = first;

    while(curr)
    {
        if(value < curr->value)
            break;

        prev = curr;
        curr = curr->next;
    }
    if(this->first == 0 || prev == 0) //if empty or add first
    {
        //first = create(key, value, this->first);
        first = new list_link(key, value, this->first);
    }
    else if(curr == 0)
    {
        //prev->next = create(key, value, 0);
        prev->next = new list_link(key, value, 0);
    }
    else
    {
        //prev->next = create(key, value, curr);
        prev->next = new list_link(key, value, curr);
    }
}

void sorted_list::remove(my_key_type key)
{
    list_link *curr = first;;
    list_link *prev = 0;

   while(curr)
   {
      if(curr->key == key)
      {
         list_link *remove;

         if(prev == 0)
         {
            first = curr->next;
            delete curr;
            curr = first;
         }
         else
         {
            remove = curr;
            curr = curr->next;
            prev->next = curr;
            delete remove;
         }
         continue;
      }
      prev = curr;
      curr = curr->next;
   }
}


sorted_list::list_link* sorted_list::clone(list_link* item)
{
   list_link* copyItem= new list_link(item->key,item->value,0);
   if(item->next!= 0)

      copyItem->next=clone(item->next);
   return copyItem;
    // ADD YOUR CODE HERE ( 4 well formatted lines in reference solution )

}

void sorted_list::copy(sorted_list* my_this_destination)
{
  if (my_this_destination->first == 0) // copy if empty
  {
      cout << "Copy" << endl;
    //list_destroy(my_this_destination);
    my_this_destination->first = clone(first);
  }
}

double sorted_list::find(int key)
{
    list_link *travel = this->first;
    while(travel)
    {
        cout << travel->key << "==" << key << endl;
        if(travel->key == key)
            return travel->key;

        travel = travel->next;
    }
    return -1;
}

int sorted_list::size()
{
    list_link *travel = this->first;
    int i = 0;
    while( travel )
    {
        travel = travel->next;
        i++;
    }
    return i;
}

sorted_list.h

#ifndef _SORTED_LIST_H_
#define _SORTED_LIST_H_

#include <iostream>

#include <iomanip>
using namespace std;

typedef int     my_key_type;
typedef double  my_value_type;

class sorted_list
{

public:
    sorted_list(); 
    ~sorted_list();

    void insert(int key, double value);
    void remove(my_key_type key);
    void copy(sorted_list* my_this_destination);
    void destroy();
    void init(struct my_list* my_this);
    void print();
    void print2();
    double find(int key);
    int  size();

private:
    class list_link // An inner class inside sorted_list
    {
    public:
        list_link (my_key_type key, my_value_type value, list_link* next = 0);
        ~list_link();
        my_key_type key;
        my_value_type value;
        list_link *next;
    };

    list_link* first;
    list_link* clone(list_link* item);
    void destroy(list_link* item);
    // More declarations

public:
    class iteratorn
    {
    public:
        iteratorn();
        ~iteratorn();
        iteratorn(sorted_list *item);
        list_link* list_begin();
        bool iterator_end();
        void iterator_next();
        int iterator_get_key();
        double iterator_get_value();

    private:
        sorted_list::list_link* current;
    };
};


#endif

iteratorn.cc

#include "iterator.h"
#include "sorted_list.h"

 sorted_list::iteratorn::iteratorn()
 {
 }
sorted_list::iteratorn::iteratorn(sorted_list *list)
{
    cout << "construct iteratorn" << endl;
    this->current = list->first;
}

sorted_list::iteratorn::~iteratorn()
{
    cout << "destruct iteratorn" << endl;
}

sorted_list::list_link* sorted_list::iteratorn::list_begin()
{
    return current;
}

void sorted_list::iteratorn::iterator_next()
{
  current = current->next;
}

int sorted_list::iteratorn::iterator_get_key()
{
  return current->key;
}

double sorted_list::iteratorn::iterator_get_value()
{
  return current->value;
}

list_link.cc

#include "sorted_list.h"


sorted_list::list_link::list_link(my_key_type key, my_value_type value, list_link* next)
{
    this->key = key;
    this->value = value;
    this->next = next;
}

sorted_list::list_link::~list_link()
{
    cout << "list_link destructor" << endl;
}
  • 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-17T23:47:21+00:00Added an answer on May 17, 2026 at 11:47 pm

    Your function void list_print(ostream& os, sorted_list list) takes a sorted_list parameter by copy. A quick and dirty fix (that you should do anyways for performance reasons) is the following:

    void list_print(ostream& os, const sorted_list& list)
    

    Now, your iteratornclass takes a mutable list, so this won’t work as you expect. You will have quite a few methods to change to make this work.

    In any case, your real problem is the lack of a proper copy-constructor. Right now, when you “copy” a list, both end up sharing the same elements, but your destructor is written as if each list owns it’s own nodes. Define a proper copy operation and it will solve your problem.

    More elaborate help on how to solve the problem: (untested)

    Change signature:

    void list_print(ostream& os, const sorted_list& list);
    

    Declare + define copy constructor:

    sorted_list::sorted_list (const sorted_list& other);
    

    Change iteratorn interface to support a const sorted_list:

    class sorted_list::iteratorn
    {
    public:
        iteratorn();
        ~iteratorn();
        iteratorn(const sorted_list& list);
        const list_link* list_begin() const;
        bool iterator_end() const;
        void iterator_next();
        int iterator_get_key() const;
        double iterator_get_value() const;
    
    private:
            // You *should* make this `const` but it is not required.
        sorted_list::list_link* current;
    };
    

    As you can see, the changes are rather minimal, but need to be applied in various places.

    const + non-const iterators:

    I applied changes here based on the fact that your iteratorn was currently only defining read-only operations on your sorted_list. If you want to support write access to allow changing the value stored in list nodes (never allow changing the key or you won’t have a sorted list anymore), you should define two iterator classes. See the STL iterator interface for more details.

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

Sidebar

Related Questions

In one of my projects, I have some classes that represent entities that cannot
I have an issue regarding freeing memory as under: string points; // some points
I have some issue with a Perl script. It modifies the content of a
I have a design issue that I would appreciate some input on. I would
We're having a small issue and could use some help - we have the
I have been using PLT Scheme , but it has some issues. Does anyone
I have inherited a middle tier system with some multi-Threading issues. Two different threads,
A client is having an issue running java2ws on some of their code, which
We have some issues in our application with skype taking over some of our
In using our TeamCity Continuous Integration server we have uncovered some issues that we

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.