C++ Class member access operator -> overloading

C++ Operator Overloading and Function Overloading

The class member access operator (->) can be overloaded, but it is rather troublesome. It is defined to give a class "pointer" behavior. The operator -> must be a member function. If the -> operator is used, the return type must be a pointer or a class object.

The operator -> is usually used in combination with the pointer dereference operator * to implement "smart pointer" functionality. These pointers are objects that behave like normal pointers, except that when you access an object through the pointer, they perform other tasks. For example, when the pointer is destroyed, or when the pointer points to another object, the object is automatically deleted.

The dereference operator -> can be defined as a unary postfix operator. That is, given a class:

class Ptr{
   //...
   X * operator->();
};

ClassPtrObjects can be used to access classes.XIts members are used in a way very similar to pointers. For example:

void f(Ptr p )
{
   p->m = 10 ; // (p.operator->())->m = 10
}

The statement p->m is interpreted as (p.operator->())->m. Similarly, the following example demonstrates how to overload the class member access operator ->.

Example

#include <iostream> #include <vector> using namespace std; // Assume an actual class class Obj { static int i, j; public: void f() const { cout << i++ << endl; } void g() const { cout << j++ << endl; } }; // Static member definition int Obj::i = 10; int Obj::j = 12; // Implement a container for the above class class ObjContainer { vector<Obj*> a; public: void add(Obj* obj) { a.push_back(obj); // Call the standard methods of the vector } friend class SmartPointer; }; // Implement a smart pointer to access members of class Obj class SmartPointer { ObjContainer oc; int index; public: SmartPointer(ObjContainer& objc) { oc = objc; index = 0; } // The return value indicates the end of the list bool operator++() // Prefix version { if(index >= oc.a.size() - 1) return false; if(oc.a[++index] == 0) return false; return true; } bool operator++(int) // Suffix version { return operator++(); } // Overload operator -> Obj* operator->() const { if(!oc.a[index]) { cout << "Zero value"; return (Obj*)0; } return oc.a[index]; } }; int main() { const int sz = 10; Obj o[sz]; ObjContainer oc; for(int i = 0; i < sz; i++) { oc.add(&o[i]); } SmartPointer sp(oc); // Create an iterator do { sp->f(); // Smart pointer call sp->g(); } while(sp++); return 0; }

When the above code is compiled and executed, it will produce the following results:

10
12
11
13
12
14
13
15
14
16
15
17
16
18
17
19
18
20
19
21

C++ Operator Overloading and Function Overloading

other extensions