That error was solved but there is a new problem,
Code:
#include <iostream>
#include <string>
using namespace std;
struct TreeNode{
TreeNode():value("A"),count(new int(1)),right(NULL),left(NULL) {cout<<"Creating"<<endl;}
TreeNode(const std::string &val):value(val),count(new int(1)){cout<<"Creating"<<endl;}
TreeNode(const TreeNode& rhs):value(rhs.value),count(rhs.count),left(rhs.left),right(rhs.right){++*count; cout<<"Impicit copy"<<endl; }
TreeNode& operator=(const TreeNode &rhs)
{
cout<<"Explicit copy"<<endl;
++*rhs.count;
if(--(*count)==0)
{
delete left;
delete right;
delete count;
}
left=rhs.left;
right=rhs.right;
value=rhs.value;
return *this;
}
~TreeNode()
{
cout<<"Destroying"<<endl;
if(--*count==0)
{
cout<<"Destroying all"<<endl;
delete left;
delete right;
delete count;
}
}
//private:
std::string value;
int *count;
TreeNode *left;
TreeNode *right;
};
using namespace std;
int main()
{
TreeNode root("root");
TreeNode root2(root);
TreeNode root3;
root3=root2;
root3.left=&root;
root3=root3;
}
in line 30 I have put a marker when the reference count goes down to 0. But when I run the program it never shows that message("Destroying all"). does that mean that those memory were never deleted? and also why cant I Self assign?