Read the member access operators in C ++ primer today. Looking at the heavy-duty arrow operator, I was a little confused at the beginning. After reading it twice, I finally understood it and wrote my thoughts here. I would like to share with you, if any, please correct me.
The arrow operator (->) is usually used to call a member of an object referred to by a pointer to a class object. The left operand is the object pointer, And the right operand is a member of the object. After defining the overload arrow operator, it looks a little special. You can call it using the pointer of A Class Object or directly using a class object.
Reload arrow operators. First, reload arrow operators must be defined as class member functions.
The arrow operator may look like a binary operator: accepting an object and a member name, and referencing an object to get a member. In fact, the arrow operator is a unary operator and does not display the form parameter (and it is a class member, and the only implicit form parameter is this ). -> The right operand is not an expression, but an identifier of the corresponding class member, which is processed by the compiler to obtain the member's work (the compiler does more things on the overloaded arrow operator than other overloaded operators, this is also a complex area ).
The following section contains the reloaded arrow operators in C ++ primer.
---------------------------------------- Gorgeous split line ----------------------------------------
Reload arrow Operators
Arrow operators are different. It may behave like a binary operator: accepting an object and a member name. References objects to obtain members. The arrow operator does not accept explicit parameters regardless of the appearance.
There is no second parameter here, because the right operand of-> is not an expression. On the contrary, it is an identifier corresponding to a class member. There is no obvious way to pass an identifier as a form parameter to a function. On the contrary, the compiler processes the work of getting members.
When writing in this way:
Point-> action ();
Because of the priority rule, it is equivalent to writing:
(Point-> action )();
In other words, we want to call the result of evaluating point-> action. The compiler evaluates the Code as follows:
1. If point is a pointer pointing to a class object with a member named action, the compiler compiles the Code as an action Member that calls the object.
2. otherwise, if point (the Chinese version is mistakenly written as Action) is an object of the class defining the operator-> operator, point-> action and point. operator-> ()-> action is the same. That is, execute the operator-> () of the point, and then use the result to repeat these three steps.
3. Otherwise, an error occurs in the code.
Constraints on return values of the reload arrow
The overload arrow operator must return a pointer to the class type, or return a class object that defines its own arrow operator.
If the return type is a pointer, the built-in arrow operator can be used for the pointer. The Compiler unreferences the pointer and obtains the specified member from the result object. If no member is defined for the type to be pointed to, the compiler generates an error.
If the returned type is another object of the class type (or a reference to this type of object), the operator will be applied recursively. The compiler checks whether the returned object belongs to a type with a member arrow. If yes, the operator is applied. Otherwise, the compiler generates an error. This process continues until a pointer is returned to an object with a specified member, or some other values are returned. In the latter case, the Code fails.
---------------------------------------- Gorgeous split line ----------------------------------------
If the content between the split lines above is understood, you don't need to read the following.
According to our understanding, three classes are defined. Class C contains class B and class B contains Class. A, B, and C define the member functions of an action. Both B and C reload the arrow operator. The difference is that B's reload arrow operator returns a pointer to a class object, while C's heavy load arrow operator returns B class objects.
Code
#include <iostream>
using namespace std;
class A{
public:
void action(){
cout << "Action in class A!" << endl;
}
};
class B{
A a;
public:
A* operator->(){
return &a;
}
void action(){
cout << "Action in class B!" << endl;
}
};
class C{
B b;
public:
B operator->(){
return b;
}
void action(){
cout << "Action in class C!" << endl;
}
};
int main(int argc, char *argv[])
{
C* pc = new C;
pc->action();
C c;
c->action();
getchar();
return 0;
}
#include <iostream>
using namespace std;
class A{
public:
void action(){
cout << "Action in class A!" << endl;
}
};
class B{
A a;
public:
A* operator->(){
return &a;
}
void action(){
cout << "Action in class B!" << endl;
}
};
class C{
B b;
public:
B operator->(){
return b;
}
void action(){
cout << "Action in class C!" << endl;
}
};
int main(int argc, char *argv[])
{
C* pc = new C;
pc->action();
C c;
c->action();
getchar();
return 0;
}
The output result of the above Code is:
Action in Class C!
Action in Class!
Code
C * Pc = new C;
PC-> action ();
The output result is
Action in Class C!
This result is easy to understand. PC is a Class Object Pointer. At this time, the arrow operator uses the built-in meaning to unreference the PC and then call the member function action of the object.
The following code
C;
C-> action ();
The output result is
Action in Class!
In fact, C-> action (); has the same meaning as C. Operator-> (). Operator-> ()-> action.
C is an object, and the arrow operators behind C use the overload arrow operators, that is, calling the operator-> () member functions of class C. In this case, the object of Class B is returned. Therefore, the operator-> () member function of Class B is called, and the operator-> () function of Class B returns a pointer, so now you can use the built-in arrow operator. The pointer returned by operator-> () of B is dereferenced, and then the member function action of the referenced object is called. In this case, the action () of Class A is called (). There is a procedure for recursively Calling operator-> (), and a built-in arrow operator is used again.
Reprinted from http://www.cnblogs.com/custa/articles/1800097.html