//This program calls unionLL function which takes list1 and list2 objects of //LinkedList type as arguments also nowhere in the function I had changed List1
//and List2 contents although the list1 and list2 head's and also their next //pointer remain same but the elem or data field is automatically updated to //some garbage value.
#include <iostream>
using namespace std;
typedef int E;
class LinkedList;
class Node
{
private:
E elem;
Node* next;
friend class LinkedList;
friend void unionLL(LinkedList list1,LinkedList list2, LinkedList& list3);
};
class LinkedList
{
private:
Node* head;
public:
LinkedList();
~LinkedList();
const Node* getHead();
bool empty() const;
bool find(const E e) const;
void display() const;
void addFront(const E e);
void removeFront();
};
When you call `unionLL()' a "copy" is created for the first and second argument
Then thing is how is that copy made.
Because you did not define a copy-constructor yourself the one that the compiler provides it used.
It would simply copy each member.
That means that both list have a `head' pointer that points to the same cell.
When the function ends, the local variables are destroyed, so list1.~List(); is invoked.
So after the function ends, you've got a list with a head that points to garbage.
So your problem is that you didn't follow the rule of three
"If you need to explicitly declare either the destructor, copy constructor or copy assignment operator yourself, you probably need to explicitly declare all three of them.|