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

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

May be interested

  • Load the operator stack to call the function () in C ++Load the operator stack to call the function () in C ++
    c ++ function call () can be overloaded for objects of class type. when you overload (), you are not creating a new way to call a function.
  • Overload binary operators in C ++Overload binary operators in C ++
    binary operators in c ++ take two parameters. you use binary operators quite often, such as addition operator (+), subtraction operator (-) and division operator (/).
  • Operator in programming COperator in programming C
    operator is a symbol that tells the compiler to perform a certain mathematical operation or logical operation. language c has a lot of operators and provides operator types.
  • Friend function in C ++Friend function in C ++
    the friend function in c ++ of a class is defined outside of that class scope, but it has access to all private and protected members of that class. even if the prototypes for friend functions appear in the class definition, friend functions are not member functions.
  • Contructor and Destructor in C ++Contructor and Destructor in C ++
    a contructor class is a special member function of a class that is executed whenever we create new objects of that class.
  • Basic C # syntaxBasic C # syntax
    c # is an object-oriented programming language. in an object-oriented programming method, a program of diverse objects that interacts with the ways of action. actions that an object can receive are called methods.
  • Wildcard representation operator in AccessWildcard representation operator in Access
    wildcard, also known as a wildcard, can be used to determine the location of a specific item when you don't remember exactly how it was written.
  • Load the stack of assignment operators in C ++Load the stack of assignment operators in C ++
    you can overload the assignment operator (=) as you can with other operators in c ++ and it can be used to create an object like copying constructors.
  • The '+' operator in SQL ServerThe '+' operator in SQL Server
    learn how to use the + operator in sql server to join two or more strings into a string.
  • How to fix CPU overload error 99%, 100% simple, effective!How to fix CPU overload error 99%, 100% simple, effective!
    how to fix cpu overload error 99%, 100%? this article will help you solve the problem of computer overload quickly and effectively!