20-CS-122-001 Computer Science II Spring 2012
Overloading ->

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev      Next      Lectures

obj->member invokes operator->() if obj is an object, not a pointer to an object
Thus: (obj.operator->())->member


       class AccountPtr {
          unsigned cnt;  // Keeps track of the number of times account accessed
          Account *ptr;
          AccountPtr(unsigned n, double b) : ptr(new Account(n,b)), cnt(0) {}

          Account* operator->() {
             return ptr;

       AccountPtr John(123, 23.34);
       cout << John->balance() << '\n';