Difference
In C + + for a class, for the members, use the dot operator. To obtain,
For a pointer to a class object, the arrow operator-> the member of the object to which the pointer points.
When the class defines-> overloaded operators, you can use either the arrow operator or the dot operator.
Overloaded-> operator
The overloaded arrow operator must be defined as a class member function. There is no explicit formal parameter (and is a member of the class, and the only implicit formal parameter is this). The right-hand operand of the-> is not an expression, but rather an identifier for the corresponding class member, which is handled by the compiler to get the member work.
The overloaded arrow operator must return a pointer to a class type, or return a class type object that defines its own arrow operator.
If the return type is a pointer, the built-in arrow operator is available for the pointer, and the compiler will dereference the pointer and get the specified member from the result object. If the type being pointed to does not have that member defined, the compiler generates an error.
If the return type is another object of the class type (or a reference to such an object), the operator is applied recursively. The compiler checks whether the returned object's type has a member arrow and, if so, applies that operator; otherwise, the compiler generates an error. This process continues until a pointer to an object with the specified member is returned, or some other value is returned, in the latter case, the code is wrong.
Code Analysis:
Here is a code to deepen your understanding.
Copy Code code as follows:
#include <iostream>
using namespace Std;
Class a{
Public
void Action ()
{
cout<< "Action in class a!" <<endl;
}
};
Class b{
A A;
Public
A A;
* Operator-> () {
Return &a;
}
void action () {
cout << "Action in class b!" << Endl;
}
};
Class c{
Public
B operator-> () {
return b;
}
void action () {
cout << "Action in class c!" << Endl;
}
};
int main ()
{
c* pc = new C;
Pc->action ();
c C;
C->action ();
return 0;
}
The above code output results are:
Action in class C!
Action in class A!
For code
Copy Code code as follows:
c* pc = new C;
Pc->action ();
The result of the output is: Action in class C!
This is because the PC is a class object pointer, at which point the arrow operator uses the built-in meaning to dereference the PC and then invoke the object's member function action.
For code:
Copy Code code as follows:
You can understand this:
C is an object, and the arrow operator behind C uses the overloaded arrow operator, which calls the operator-> () member function of Class C. At this point, the object of Class B is returned, so the operator-> () member function of Class B is invoked, and B's operator-> () returns the pointer, so you can now use the built-in arrow operator. Dereference the pointer returned by the operator-> () of B, and then invoke the member function action of the object after the Dereference, at which point the action () of Class A is invoked. There is a procedure for recursively calling operator-> (), and finally, an arrow operator with built-in meaning.