Dereference and arrow operators for operator overloading

Source: Internet
Author: User

The arrow operators are different. It might behave like a two-dollar operator: Accept an object and a member name, and dereference the object to get the member. the arrow operator does not accept explicit parameters, regardless of appearance. There is no second parameter, because the right operand----is not an expression, instead it is an identifier that corresponds to a class member. There is no obvious way to pass an identifier as a parameter to the function, instead, the compiler handles the work of getting the member.

Understanding 1
when this is written: Point->action ();because of the priority rule, it is actually equivalent to writing: (Point->action) ();In other words, what we want to invoke is the result of the point->action evaluation. The compiler evaluates this code:
1. If point is a pointer to a class object that has a member named action, the compiler compiles the code to invoke the object's action member.
2. Otherwise, if point is an object of a class that defines the operator-> operator, Point->action is the same as point.operator-> ()->action .That is, execute the operator-> () of point, and then use that result to repeat the three steps.
3. Otherwise, the code is faulted.

Understanding 2
arrow operators, generally have left and right two parts:a->b;How to get started: Starting from A, divide the following two situations:
1, A is a pointer, then we are familiar with, point to our type a member data or function "B"; here, it's over!
2, A is the object, then a must have the member function "operator->" (otherwise it will be an error). Then call A's operator-> function. Since operator-> may return a pointer,It could also be an object, then it went into the next round of recursion: A pointer or an object, walking 1 or 2, so recursive. How to end: Whether a is a pointer or an object, the end is to go to the end of the pointer.

Understanding 3
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 references the pointer and obtains the specified member from the result object. If the type being pointed to does not have that member defined, the compiler produces 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 to see if the object's owning type has a member arrow, and if so, applies that operator; otherwise, the compiler produces an error. The process continues until a pointer to an object with the specified member is returned, or some other value is returned, in which case the code goes wrong.

Example
#include <iostream>using namespace Std;class d{public:void info () {cout << "I am D" << end l;}};          Class c{Public:void info () {cout << "I am C" << Endl;}          d* operator* () {return &m_d;}     d* operator-> () {return &m_d;} Private:d m_d;};          Class b{Public:void info () {cout << "I am B" << Endl;}          c& operator* () {return m_c;}     c& operator-> () {return m_c;} Private:c M_c;};          Class a{Public:void info () {cout << "I am A" << Endl;}          c* operator* () {return &m_c;}     c* operator-> () {return &m_c;} Private:c M_c;};     int main () {a A;     A->info ();     A->operator-> ()->info ();     b b;         B->info ();     B *BP = &b;     Bp->info ();     A *ap = &a;         Ap->info ();         cout << Endl << "deference test" << Endl << Endl; ADa     (*da)->info ();     B DB;     (*DB)->info ();     (*db). info ();     (**DB)->info ();     C DC;     (*DC)->info (); return 0;}

Run ResultsI am C
I am D
I am D
I am B
I am A

Deference test

I am C
I am D
I am C
I am D
I am D
Results AnalysisThe arrow operator and the reference operator are overloaded only on class objects, acting on the pointer and handling the meaning of the original original ecological pointer.
1, A->info ();
First, A is an object, and there is a operator-&gt, and the call to return c* is a pointer. So, the next step is to call the function info and find the corresponding C member function.
2, A->operator-> ()->print ();
A call to the returned c*, which is a pointer, is then called C's member function. Next is a operator-> (), which is just a member function of C.
C's operator-> return is d*, is a pointer, then call the corresponding D member function info ();
That is, the operator-> () in "a->operator-> ()->info ()" needs to be considered as a member function of C (just return d*, can invoke the arrow "-") to understand.
3, B->info ();
B is the object, call Operator-> return C object, then need to recursion again; C is the object, call operator-> return d*, then is the pointer, you can go to the end: Call the corresponding function info.
4, Bp->info ();
The BP is the pointer, and the next step is to call the function info and find the corresponding B member function
5, Ap->info ();
The AP is a pointer, and the next step is to call function info to find the corresponding a member function
Dereference Analysis: 6, (*da)->info ();d A is an object of type A, (*DA) Call operator* Returns a pointer to type C, a post-arrow operator operation rule, 7, (*DB)->info () and (*db). info (); The DB is the object of B (*DB) that returns a reference to an object of type C, i.e. (*DB) is an object of type C,-> after the same-arrow operator operation rule; the. operator directly invokes the member of the object. 8, (*DC)->info ();d C is an object of Type C, (*DC) Call operator* Returns a pointer to type D, followed by the arrow operator operation rules;
The above example is just for the sake of understanding the dereference operator and the arrow operator, and there is no practical value.in STL source code, OPERATOR* returns an object's reference,operator-> returns a pointer to the object, where the object corresponds to the element in the container.

Dereference and arrow operators for operator overloading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.