I have written some classes in my code, and when I create object pointers of it, I want to assign one to another. I think I must write an operator= function to overload it. But I don't know how to do it with pointers. My class' constructor is:
1 2 3 4 5 6 7 8 9 10
ExpNode::ExpNode(){
this->value=0;
this->type=' '; // a number or operator? N or O?
this->op=' '; // operator: + - / * (^)
this->var=' '; // A, B, C etc.
this->left=NULL;
this->right=NULL;
}
and I want to do something like:
1 2 3 4 5
deque <ExpNode*> treedeque(exp.size()); // creating a deque with size of a string
ExpNode *x=new ExpNode;
treedeque[i]->value=value; // assigning an integer to the value field of ith element of treedeque
x=treedeque[5]; // assigning the 5th element of the deque to x
Well you are copying the pointers which are integral values. Therefore the objects operator= isn't invoked when you copy only pointers. TO deep copy you'd do something like this.
*x = *(treeDeque[5]);
Of course that assumes that you have defined an assignment operator for the ExpNode class. Take a look at this article. http://cplusplus.com/articles/jsmith1/
Do I have to overload operator=, or does it automatically have one when I don't?
Because it says "First, you should understand that if you do not declare an
assignment operator, the compiler gives you one implicitly. The
implicit assignment operator does member-wise assignment of
each data member from the source object"
You may not need to define one for your nodes but I don't really know what your project does. The default does a shallow copy so the left and right pointers would be copied by the default and you'd end up with multiple nodes with the same left/right pointers. If you want those deep copied you have to write your own but I'm not sure why you need the copies so I can't make that decision for you.