Overload class member access operator (->) in C ++

The class member access operator (->) can be overloaded, but it's quite complicated.

The class member access operator (->) can be overloaded, but it's quite complicated. It is defined to provide a class type of a pointer-like behavior. Operator -> must be a member function. If used, its return type must be a pointer or an object of a class that you can apply.

The -> operator is often used in conjunction with the * operator to deploy "smart pointer". These pointers are objects that operate like regular pointers, except that they perform tasks when you access an object through them, for example, deleting the object automatically or when the cursor is canceled or when the cursor is used to point to another object.

Operator -> can be defined as a single suffix operator.

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

Objects of Ptr class can be used to access the members of the X class above in the same manner as the pointers used. For example:

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

The command p-> m is translated into (p.operator -> ()) -> m. Using the same concept, the following example explains how a class-> access operator in C ++ can be overloaded.

 #include #include using namespace std ; // gia su co mot lop QTM sau. class QTM { static int i , j ; public : // cac smart pointer de hien thi void f () const { cout << i ++ << endl ; } void g () const { cout << j ++ << endl ; } void h () const { cout << "--------" << endl ; } }; // phan dinh nghia cac thanh vien Static: int QTM :: i = 4 ; int QTM :: j = 15 ; // Trien khai mot container cho lop tren class VJContainer { vector < QTM *> a ; public : void add ( QTM * vj ) { a . push_back ( vj ); // goi phuong thuc chuan cua vector. } friend class SmartPointer ; }; // trien khai smart pointer de truy cap thanh vien cua lop QTM. class SmartPointer { VJContainer vc ; int index ; public : SmartPointer ( VJContainer & vjc ) { vc = vjc ; index = 0 ; } // tra ve gia tri de chi phan cuoi cua danh sach: bool operator ++() // phien ban toan tu ++ (tien to) { if ( index >= vc . a . size ()) return false ; if ( vc . a [++ index ] == 0 ) return false ; return true ; } bool operator ++( int ) // phien ban toan tu ++ (hau to) { return operator ++(); } // nap chong operator-> QTM * operator ->() const { if (! vc . a [ index ]) { cout << "Gia tri 0!!" ; return ( QTM *) 0 ; } return vc . a [ index ]; } }; int main () { const int sz = 5 ; // so vong lap la 5 (ban thiet lap gia tri khac de xem ket qua) QTM o [ sz ]; VJContainer vc ; for ( int i = 0 ; i < sz ; i ++) { vc . add (& o [ i ]); } SmartPointer sp ( vc ); // tao mot iterator do { sp -> f (); // goi smart pointer sp -> g (); sp -> h (); } while ( sp ++); return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Overload class member access operator (->) in C ++ Picture 1

According to Tutorialspoint

Previous lesson: Overloading subscript operator [] in C ++

Next article: Polymorphism in C ++

5 ★ | 1 Vote | 👨 230 Views
« PREV POST
NEXT POST »