The overload of operator----is special, it can only be a non-static member function, and there are no parameters.
1. If the return value is a raw pointer, the right operand of the operator is accessed as a member of the type pointed to by the original pointer;
2. If the return value is an instance of another type, continue calling operator-> ()of the return type until one of the calls returns a raw pointer, and then the first case is processed.
If the above conditions are not met (for example: the right operand is not a member of the type to which the original pointer is returned, or if the returned non-pointer type (an instance of another type) does not overload operator-> ()), then the compilation will error.
The following are the program fragments for validation:
/******************************************************** File name:main.cpp* Description: Show overloads for member access operators * version:v1.0 * Author:mengjia * date:2018-05-20 * Copyright (C) 2018, mengjia* others:********************************** The overload of operator----is special, it can only be a non-static form of a member function (that is, it cannot be overloaded outside the class), and there are no parameters. 1, if the return value is a raw pointer, then the right operand of the operator as the original pointer to the type of the member to access, 2, if the return value is another instance of the type, then continue to call the return type of operator-> (), Until one of the calls returns a raw pointer, it is then processed in the first case. If the above conditions are not met (for example: the right operand is not a member of the type to which the original pointer is returned, or if an instance of another type returned does not have an overloaded operator-> ()), then the compilation will error. *************************************************************************************************************** **********************/#include<iostream>using namespacestd;//Original Classstructorigin{intA;};//wrapper Class 1, Packaging Origin classstructwrapper{Origin*orig; Origin*operator()Const { returnOrig//returns the original pointer }};//wrapper class 2, Packaging wrapper classstructwrapper2{Wrapper*Wrap; Wrapper&operator()Const { return*wrap;//return class object }};intMain () {Origin o; O.A=7; Wrapper W; W.orig= &o; Wrapper2 W2; W2.wrap= &W; cout<<"W->a"<<"\t\t\t\t"<< W->a <<Endl; cout<<"w.operator-> ()"<<"\t\t\t"<< W.operator() <<Endl; cout<<"w.operator-> ()->a"<<"\t\t"<< W.operator()->a <<Endl; cout<<"W2->a"<<"\t\t\t\t"<< W2->a <<Endl; cout<<"&w2.operator-> ()"<<"\t\t"<< &w2.operator() <<Endl; cout<<"w2.operator-> ()->a"<<"\t\t"<< W2.operator()->a <<Endl; cout<<"w2.operator-> () .operator-> ( )"<<"\ t"<< W2.operator().operator() <<Endl; cout<<"w2.operator-> () .operator-> ()->a"<<"\ t"<< W2.operator().operator()->a <<Endl; System ("Pause"); return 0;}/** Output Result: W->a 7w.operator-> () 00aff7e4w.operator-> ()->a 7w2->a 7&w2.operator-> () 00aff7d8w2.operator-> ()->a 7w2.operator-> () .operator-> () 00aff7e4w2.operator-> () .operator-> ()->a 7**/
One of the most bizarre is that the w2->a output is 7.
According to the conclusions summarized above, this call is actually converted to the w2.operator-> () .operator-> ()->a form, so the output is 7.
Transfer from C + + to member access operator, overload
"Go" overloads of C + + for member access operators