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
this->type=' '; // a number or operator? N or O?
this->op=' '; // operator: + - / * (^)
this->var=' '; // A, B, C etc.
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; // assigning the 5th element of the deque to x
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.