struct T
{
int a;
int b;
};
class Ptr
{
public:
Ptr(int a, int b) { t_.a = a; t_.b = b; }
T* operator->() {return &t_;}
T& operator*() {return t_;}
private:
T t_;
};
int main()
{
Ptr ptr(1, 2);
cout << "a = " << ptr->a << " b = " << ptr->b << endl;
cout << "a = " << ptr.operator->()->a << " b = " << ptr.operator->()->b << endl;
}
Output:
a = 1 b = 2
a = 1 b = 2
Why is ptr->a
the same as ptr.operator->()->a
, what's the principle in it?