The difference is that operator->
can be overloaded to return multiple levels of proxy objects with overloaded operator->
to which it is then again applied recursively, until a plain pointer is returned, like in Wrapping C++ Member Function Calls by Bjarne Stroustrup.
Whereas operator.
cannot be overloaded in C++.
The example from that paper is:
#include<iostream>
using namespace std;
void prefix() { cout<< "prefix"; }
void suffix() { cout<< " suffix\n"; }
template<class T>
class Call_proxy{
T* p;
public:
Call_proxy(T* pp) :p(pp){ }
˜Call_proxy() { suffix() ; }
T* operator->() { return p; }
};
template<class T>
class Wrap{
T* p;
public:
Wrap(T* pp) :p(pp) { }
Call_proxy<T> operator->() { prefix() ; return Call_proxy<T>(p) ; }
};
class X{ // one user class
public:
X() { cout<< "make an X\n"; }
int f() const{ cout<< "f()"; return 1; }
void g() const{ cout<< "g()"; }
};
class Y{ // another user class
public:
Y() { cout<< "make a Y\n"; }
void h() const{ cout<< "h()"; }
};
int main() // simple test code
{
Wrap<X> xx(new X) ;
Wrap<Y> yy(new Y) ;
if(xx->f()) cout<< "done\n";
xx->g() ;
yy->h() ;
return 0;
}
Each call of xx
and yy
is bracketed by a pair of prefix()/suffix() calls, so the program produced:
make an X
make a Y
prefix f() suffix
done
prefix g() suffix
prefix h() suffix