Question - Unary Operator Overloading in Class


I am trying to overload dereference * in class node_itr. How do I know if the overloaded * operator will be bound to a left operand or a right operand?

I understand that for an in-class overloaded operator, the object itself is underlying. However I don't know how to distinguish the position of binding. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template<typename T>
class node_itr
{
public:
    // constructor omitted
    T& operator*() const {
        return data;                        
    }
private:
    T data;
};

#include<iostream>
int main()
{
     node_itr i;
     std::cout<<*i;
     std::cout<<i*;      //how do I tell if this is also legal? 
     return 0;
}
Last edited on
Unary * is always a prefix operator. The expression i* is never legal.
So does that mean overloaded unary operators always follow their original binding sequences?
Yes. The binding, precedence, associativity, and order of evaluation are unchanged for overloaded operators, as far as I know.

Since the associativity and order of evaluation are usually undefined, that part doesn't make a lot of difference.
Awesome, Thanks guys! :)
Topic archived. No new replies allowed.