3.1 Data member binding

Source: Internet
Author: User
The article begins with a sample code and discusses the problem of which X is returned. Then

A) give answers that we generally think are correct, and affirm this idea;

B) remind everyone that this is not correct in the previous compiler implementation.

In earlier implementations, the "global variable X" was first bound ". Because the declaration of X in this class is not visible yet. This leads to "defensive programming style ". To sum up, there are two specific practices.

C) advance all data member statements;

D) Move the implementation of inline functions out of class declaration.

We can easily understand the first point. In the Declaration of variables, a later declaration can always overwrite the previous one. I want to explain my understanding of the second point.

This is called "member rewriting rule" in the book ". Basically, the entity of the inline function will not be evaluate when the entire class is not fully declared. (In this book, evaluated is translated as an evaluation value. I personally think it is not very appropriate, I have not come up with any better translation, but I still want to make it more accurate in English. In addition, the resolve in the Post article is not very fluent in translating it into "resolutions" and I think "resolution" is better ). That is to say, the data member binding in the inline function is also carried out after the class declaration. This meaning is extracted by C ++ standard as "member scope resolution Rules" (member scope resolution rules ). This is what we say in the book. In the program, if the inline function is expanded immediately after the class declaration, it will be evaluate.

Of course, the above rules also expire, for example, the parameter list of member functions. The rule they follow is "the first time they met, they were completed by appropriate resolutions ". Later, the author said, "non-intuitive binding between extern and nested type names will still happen ". The purpose of this statement is to resolve "nested type names" to the extern type. This can be cited using the instance code in the book.

Before the definition of length appears, it is used twice. The result is that length is considered to be of the extern type and conforms to the preceding analysis. Later, when the length statement appears, the above resolution will be denied. As mentioned in the original article, "C ++ standard marked earlier binding as illegal ". The meaning of this sentence is not clear. What does it mean that the mark is invalid? Compilation fails, but compilation passes, but the compilation overhead is increased (because length is considered to be a type, but the subsequent declaration makes the original determination wrong, and re-PARSE ). In this case, the authors suggest using a defensive programming style, which means we recommend that you advance the data member statement to use. From this point of view, can we think that the C language programming method is more scientific?

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.