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

   Example:

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

          Account* operator->() {
             cnt++;
             return ptr;
          }
       }


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