That was exactly my own problem.
When you dynamically allocate memory for pointers( as a filed of your object) and your program have functions and operators that accept objects as arguments, you need to create a copy constructor. Why?
Two important parts that trigger the copy constructor are:
1-passing objects to functions
2-functions that return objects
Think in this way:
Because objects are data types that contain other data types in themselves, in these two situations they need to copy their contents to the corresponding variable in the function's parameter list. Even if a function's parameters are reference type (like your = and + operators).
To speak generally, when these types of functions are called through your program, copy constructor comes to the scene. If you haven't defined a copy constructor, a default copy constructor is created by the compiler and initializes your objects in these functions. The results is loss of data like unexpected number you get through your program execution
Here is the definition of a copy constructor for your class a:
Note that copy constructor always gets a reference to object of it's own class.
This is the implementation:
node = new b;
Remember that copy constructor calls the destructor automatically. Because of that, we did not write "delete node;" at the beginning.
So, be sure to add these codes to your class a
I hope you understood the copy constructor.There are lots written about it in many books.
Thanks for such a good question !!!!