Question - Unary Operator Overloading in Class

Jul 29, 2019 at 8:43pm

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 Jul 29, 2019 at 9:32pm
Jul 29, 2019 at 9:04pm
Unary * is always a prefix operator. The expression i* is never legal.
Jul 29, 2019 at 9:33pm
So does that mean overloaded unary operators always follow their original binding sequences?
Jul 29, 2019 at 9:43pm
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.
Jul 29, 2019 at 10:28pm
Awesome, Thanks guys! :)
Topic archived. No new replies allowed.