Y JIANG 0 Newbie Poster

Hi all, I'm reading the book Accelerated C++ and encountered a problem described on P256&P257. Here is the code fragment:

template <class T> 
class Handle
{
public:
Handle():p(0){}
Handle(const Handle& s):p(0){if (s.p)p = s.p->clone();}
Handle& operator=(const Handle&);
~Handle(){delete p;}

Handle(T* t):p(t){}

operator bool() const {return p;}
T& operator*() const;
T* operator->() const;

private:
T* p;
};


template <class T>
T* Handle<T>::operator->() const
{
if(p) return p;
throw runtime_error("unbound Handle");
}

In the book ,It's said that 'if x is a value of type that defines operator-> ,then, x->y is equivalent to (x.operator->())->y. ' But where there are two -> operators being called? In my view, -> is left associative.Thus,when implementing x->y , x-> returns a pointer p,which means x->y is equivalent to p y. So how could the operator -> between p and y appear?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.