I know how to overload operators now, but now I want to know if there's an operator function to combine the [] and = operators to change the value of an encapsulated piece of data, like a node in a queue/stack.
Any suggestions would be great and examples even more welcome.
struct node
{
node* next;
node* prev;
int data;
};
Would the [] operator return the data, and the = operator is programmed to modify the data in the node?
1 2 3 4 5 6 7 8 9 10 11
// In the main object
intoperator[](int index)
{
return array[index]->data; // Array holding address values of nodes
}
// In the node object
voidoperator=(int newdata)
{
data = newdata;
}
The compiler doesn't resolve everything at the same Time. It does it in steps. In this case it resolves the the [] operator first to returns reference and then assigns the value. Because c++ operates automatically on predefined types, you don't need to overload the = operator. You would though if it returned a user defined type.