Destructors being called twice in inheritance

[EDIT]: Problem solved. It is due to that I didn't put the copy constructor and also the function of copy list

I found this same question a lot over the net none of it provides a solution. It only explains why the destructors being called twice.
So I faced this problem here for linked list when I have to delete the pointer.

Here's the destructor for unordered linked list which is the base class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<class Type>
UnorderedLinkedList<Type>::~UnorderedLinkedList()
{
    node<Type> *current, *temp;
    current = first;
    while (current != NULL)
    {
        temp = current;
        delete temp;
        current = current->link;
    }

    last = NULL;
    count = 0;
}  


Here's the destructor for ordered linked list which is the derived class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<class Type>
OrderedLinkedList<Type>::~OrderedLinkedList()
{
    node<Type> *current, *temp;
    current = this->first;
    while (current != NULL)
    {
        temp = current;
        delete temp;
        current = current->link;
    }

    this->last = NULL;
    this->count = 0;
}


And it so happens that in my main program that I declare two objects:
1
2
UnorderedLinkedList<Data> unorderedDataList;
OrderedLinkedList<Data> orderedDataList;


The program crashes when I exit the program. Is there any solution for this?
Last edited on
Ask yourself: Who owns the nodes? There can be only one.

Why should the ordered list even know that there are nodes? It should use the interface of the unordered list.
I made two lists (just to fulfill assignment's criteria) that's why I created two objects.
I've already deleted the destructor for ordered linked list since unoredered linked list contains the nodes and all. But still the problem occurs.

I found on the net that when an object of the inherited class is declared, the constructor is called twice and so do the destructor.
I found on the net that when an object of the inherited class is declared, the constructor is called twice and so do the destructor.

The meaning of "twice" might not be what you think.

Please, show the definitions of your two template classes. (No member implementations yet.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template<class Type>
struct node
{
    Type data;
    node *link;
};

template<class Type>
class UnorderedLinkedList
{
protected:
    int count;
    node<Type> *first;
    node<Type> *last;
public:
    int returnCount();
    void insertFirst(const Type& newObject);
    void insertLast(const Type& newObject);
    void insertNode(const Type& newObject, int getPosition);
    bool searchByReg(int getRegNo);
    void displayParticular(int getRegNo);
    void displayAll();
    void deleteParticular(int getRegNo);
    void deleteAll();
    void categorize();
    void saveObjectInfo(ofstream& outfile);
    UnorderedLinkedList();
    ~UnorderedLinkedList();
};


1
2
3
4
5
6
7
template<class Type>
class OrderedLinkedList: public UnorderedLinkedList<Type>
{
public:
    void insertNode(const Type& newObject);
    void displayRestriction(int restrictType, double amountRestrict);
};


Basically, my program works like this:
If user choose to enter information into ordered linked list, then it will sort accordingly to its value (price in this case) whereby if user choose to enter information into unordered linked list, then it can choose to insert by position for the nodes.
Last edited on
We can clearly see that the OrderedLinkedList has no data members of its own. The members of UnorderedLinkedList should preferably be private, not protected.

When an OrderedLinkedList object is created:
1. Allocate enough memory for the whole object
2. Initialize the UnorderedLinkedList subobject
2a Initialize the members of UnorderedLinkedList (data* and link)
2b Execute the body of UnorderedLinkedList<Type>::UnorderedLinkedList
3. Initialize members of OrderedLinkedList (none)
4. Execute the body of OrderedLinkedList<Type>::OrderedLinkedList

On destruction:
1. Execute the body of OrderedLinkedList<Type>::~OrderedLinkedList()
2. Destruct members of OrderedLinkedList (none)
3. Destruct the UnorderedLinkedList subobject
3a. Execute the body of UnorderedLinkedList<Type>::~UnorderedLinkedList()
3b. Destruct the members of UnorderedLinkedList (data* and link)
4. Deallocate the memory block

*) The Type naturally may have constructor, destructor and base class(es) of its own


Your design has a serious flaw:
1
2
3
4
OrderedLinkedList<int> order;
order.insertFirst(  7 );
order.insertFirst( -1 );
order.insertFirst( 42 );

That is syntactically legal, but is the list ordered?
Topic archived. No new replies allowed.