function lookup in C + +

Source: Internet
Author: User

Template <classT>classvisibility{ Public:voidSayDoubled) {};Private:voidSayinti)  {}; voidsay (T-t) {};};int_tmain (intARGC, _tchar*argv[]) {Visibility<Char*>v; V.say (123);//error C2248: ' Visibility<t>::say ': Cannot access private member declared in class ' visibility<t> ' //overload resolution: void Say (int i) {};V.say ("123");//error C2248: ' Visibility<t>::say ': Cannot access private member declared in class ' visibility<t> ' //overload resolution: void Say (T t) {};    return 0;}
0 Access Restrictions:
1 Private :   Its name can only be used by members and friends of the class being declared.  2protected: Its name can be used by members and friends of the class being declared, or it can be used by members and friends of the derived class 3public:    Its name can be used indefinitely anywhere.
1 occurs before the call

CPP file called a function (or member function), the compiler mainly does the following four things:

1).Name lookup (name lookup)

Identify a range of candidates. (Note that the ' name ' in ' name lookup ' is simply a function name, not a function signature, that is, not including the return value, parameter)

(1) Scope search, once the scope is determined, the stop range is searched.

A) If there is a qualified name (X::FUNC), then look in X::; Can not find the error;

b) If it is a member function (OBJECT->FUNC), then find in the class, cannot find, then go to the base class to find; Can not find the error;

c) If the function is shaped like ' func () ', the lookup rule is as follows:

<c-1> in the class where the outer function (caller) is found; If you can't find it, go to <c-2>;

<c-2> found in the base class, cannot find the outer base class to find (BaseClass, outer baseclass ...); Stop searching once it is found; If you can't find it, go to <c-3>;

Eg:void A::say () {func (), go to the say in the Class A to find, can not find a base class to find.

<c-3> find in the current namespace; Can not find the outer namespace to find (the current namespace-> outer layer of namespace, the outer namespace ...); Stop searching once it is found; Can not find the error;

Note: If the function argument is class/struct, the namespace of the parameter type will also be listed as the lookup scope because of the koening query. So the names in the namespace of the parameter type are also included in the candidate range.

Eg: here the ' func ' was found in the namespace Lv2, and the ' func ' was found through the koening in Koeningns, so (III) the candidates found were: {(I) Koeningns::func , (II) Lv3::lv2::func }

namespacekoeningns{classKCLS {}; void func (KCLS) {}                  //(I)}namespaceLv3
{ namespaceLv2
{ void func (KOENINGNS::KCLS) {} //(II) namespaceLV1 {classMycls { Public : voidsay () {func (Koeningns::kcls ());//(III)//error C2668: ' Lv3::lv2::func ': ambiguous call to overloaded function } }; } }}voidTest_nn () {lv3::lv2::lv1::mycls obj; Obj.say ();};

<c-4> finally identified candidates: {candidates in class} or {candidates in base class} or {namespace candidates}

Note: Once the compiler determines a candidate collection, it stops looking. For example, if you find the candidates in the {class}, then you will not be concerned {The candidates in the base class} will not be interested in {namespace}.

(2) Identify a number of candidates in the range found

For example, if you find say in class, you can identify the candidate:

void Say (double D);

void say (int i);

2.overload resolution (overload resolution)

The compiler starts executing overload resolution and selects the most matching function from the candidate according to the principle of parameter best match. If it is not unique, there is ambiguity.

Eg:int Say (int i) is selected.Priority level:1) Non-template function2) If the compiler does not find a suitable non-template function, then the main function template is taken into account.   function template specificity is not involved in overload resolution. Only if a master template is overloaded with a resolution, its special version is likely to be used, and the compiler does not care whether or not he has a specific version when choosing the main template.because the function-specific template does not participate in overload resolution, the best way to use a particular function is to overload the function. 3.accessibility checking (accessibility detection).The compiler performs accessibility detection to determine whether the selected function can be called.overload resolution occurs before accessibility checks. Therefore, if the private function unfortunately participates in the overload and is selected, there will eventually be an unreachable compilation hint.this often implies ambiguity, and the design itself is unreasonable. In other words, private parameters are not private when names are searched and overloaded.Note that for template functions, this phase only detects the main template function, not the special form of the template function. That is, if the main template function cannot be accessed, even if the template function's special version is public, it is useless. 4. If the template function is selected, then you will also have to make templates to special resolution
classctemp{ Public: Template<typename t>voidSayHello () {cout<<"SayHello: main template"<<Endl; }Private: Template<>voidsayhello<int>() {cout<<"SayHello: Special Templates"<<Endl; }Private: Template<typename t>voidSaybonjour () {cout<<"saybonjour: main template"<<Endl; } Public: Template<>voidsaybonjour<int>() {cout<<"Saybonjour: Special Plate"<<Endl; }};voidTest_nn () {Ctemp ct; Ct.sayhello<int> ();//main template public, special template private//You can call the private of the special.ct.saybonjour<int> ();//error C2248: ' Ctemp::saybonjour ': Cannot access private member declared in class ' Ctemp ' 
    //main template Private, special template public//The main template is considered ' inaccessible ' when it is ' accessibility check ', even if the template public is special. };

function lookup in C + +

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.