A small study on the C ++ name resolution rule,

zhaozj2021-02-16  100

The name analysis process in C is probably one of the troubles. I spent a lot of time, I didn't dare to understand myself, but I have to write some things you have to understand, so that I will finish the idea. If you don't pay, please pay more.

There is no doubt that most of the work is completed by the compiler, but it knows some to help us apply to some language mechanisms. For example, overload, derived mechanism, etc., therefore spend some time still necessary, below I tried to pass a small code to take this process in order, the code is as follows:

#include "iosrtream"

Using namespace std;

Namespace a

{Struct tst {int sht;};

Void F (TST &);

Void f (void);

INT A = 11;

}

INT A = 100;

void main ()

{

A: TST ABC;

f (ABC); // <1>

f (); // <3>

INT A = 10;

COUT << a << endl; // <2>

For (int i = 0; i <1; i)

{

INT A = 1;

COUT << a << endl; // <4>

}

}

Void A :: F (TST & A)

{

Cout << a.sht << Endl;

}

Void a :: f ()

{

COUT << "f ()" << endl;

}

The first step, the name is found. For variables, this process is not complicated. The search order is a lookup mode from the local domain to the global domain, where it is important to pay attention to the name "hide" The name of its outer fence in a domain. And the names in the domains that are inclusive are unable to understand, and it is not difficult to understand, <2>, <4> can be compiled, and the output structure is not the same, "4": for a circulation A The A and Global Domain in Main () is hidden: "2": Hide a full-local domain A, and A is not visible here. If you remove the A definition in the current domain, look for the peripheral domain, Once you have stopped immediately, the upper three A definitions do not cause erliness, and in addition, there is a similar relationship in the derived mechanism, subclasses and base classes. The name of the subclass will hide the same name of the base class.

For functions, the name lookup rules are complex, "1". One pair of "3", but we found that the namespace domain A is not visible in the main function, and the function definition is after the main function. Why is "1" right. "3" wrong? We see the only difference is "1" has a TST-ginseng, C lets the field defined by the function parameter type, also added to the search range of the function name resolution, this rule is called the Koenig name lookup rules for specific visits:

Http://h21007.www2.hp.com/dspp/tech/tech_techdocumentdetailpage_idx/1,1701,990,00.html

For functions, the name resolution still has a second step: I have been very strange, why can C support function overload, can the connector make the same name passed? Is the implementation of the connector changed? That year, the risk of B, s is too big. According to the query, I found that there is a mechanism that makes the compiler to change the same name to the different names to the connector. Thus, the same name function is overloaded, this is the name reorganization mechanism, such as the above two F () pass the compiler to f_tst ((TST &) and F_void () to send the connector, so it is not at the connector There is a function of function name overload. About Name Mangling (Name Regeneration), and Koenig Name Lookup (Koenig Name Find Rules). I really have to study more carefully, I hope to have the opportunity to add this text later.

转载请注明原文地址:https://www.9cbs.com/read-13642.html

New Post(0)