The "Trinity" interpretation of C + + Reference

Source: Internet
Author: User
Tags shallow copy

C + + is a "all-in-one" language between assembly language and high-level languages. Its ability is that any other high-level programming language based on the VMA(von-Norman architecture) computer cannot be matched, and the performance of only C language can be comparable.

However, for a long time, like C + + and hate C + + camp, fight for more than 30 years, who can not persuade anyone. One of the important reasons is that the logic and semantics of C + + are hard to split, and it is difficult for computer academics to formalize it (such as having a mathematical beauty like Haskell ), which is similar to the uncertainty in quantum mechanics, which Einstein hated.

I do not want to comment on the right and wrong of the two sides, but I would like to make a discussion on the "reference" of one of the most basic concepts of C + +, one of the most important concepts, to see if it makes sense.

If a reader reads "http://blog.csdn.net/ly8838/article/details/38638491", it may resonate with the subtle side of reference . Here I would like to talk a little bit further about the details of reference .

To describe reference, we have to start with a more basic definition- direct object .

In C + +, the direct object is the "normal variable" (normal variable), with the value semanticssemantics, and the rvalue semantics. This has not been introduced into the reference, has been in the logic of "circular definition" of the vicious circle. To jump out, there are only examples:

long x;

MyClass m; MyClass is a user defined class

The above x and m are " Direct variables ", and they can be different in memory: they can be in the static space of the program (full variable), within the work stack (automatic variables), It can also be contained within a dynamic space (indirectly, in a parent object). These direct variables have a significant impact on the performance characteristics of the program due to the different areas in which they are located (later blog mentions). But most importantly, they are similar to the traditional "basic variant (primitive type variable)" in syntax and usage . This is an important reason why C + + supports it.

For programmers who first learn c#,javascript, or other modern languages, " Direct variables " have little or no corresponding concept. What is even more puzzling is that in dynamic space (heap), Direct variables exist only " indirectly ". The only way to "directly use" dynamic variables is to " pointer " and "reference". This is again a loop: When interpreting direct variables, you cannot bypass the " pointers " and "reference".

So what exactly is reference ? Looking at the micro-base interpretation, (Http://en.wikipedia.org/wiki/Reference_ (C%2B%2B)), it is almost agreed to repeat, and finally only with "metaphor" to explain. In other words, no "pointers", "memory address" and other concepts can not be explained.

So, the popular saying "reference " is the alias of the variable (the same body name?). ) "Does that really work?"

I thinkalias is the best metaphor for beginners. Please see:

int x = int (10); line1

int & r = x; line2: R is an alias of X

r++; line3: x is now 11

R = 3; line4: X is now 3

ASSERT (&r = = &x); Line5: R points to X

We see that the above program shows thatR and x are almost indistinguishable, as if they were shadow, inseparable. This is the purpose of C + +: Let reference variables and direct variables have "equivalent" syntax, usage.

As a novice programmer, it is enough to know this usage, but "a little bit of knowledge is dangerous". If we think that x and R are a thing, it's wrong:

First,x is a "direct variable", and R is an indirect variable, so they are semantically completely different (the compiled code is large-diameter). You can say it's not about me, it's going to work, and that's fine, but it's not easy to be comfortable: In another environment, you might be wrong about the difference between object reference and direct object, or write programs with vastly different performance .

Semantically,line2 is almost equal to R = &x (the address of X), no wonder many people think reference is actually a pointer, and line5 Prove that it does point to its "direct variables". Knowing this is important for the following code:

Class X {...}

void Myxfunction (x x) {...}

void MyXFunction2 (const x & x) {...}

If you think x& is the "alias" of X , and you use the same, you probably don't need myXFunction2, but if you know x& Semantics and x * , you can infer that myXFunction2 performance is generally better than myxfunction , especially when X contains a large number of "members", myXFunction2 not copy content, just copy address (we say this is a shallow copy -shallow copy), and Myxfunction The content that must be passed in is copied into the argument variable (we say this is a deep copy-deeply copy).

Here, the semantics cover the logic. The use of const X &x , especially semantics, is best explained by pointers.

Well, then say reference is a pointer, but it does not solve the above program line4 Usage:

Because assigning a value to a pointer causes it to change direction. If r is a pointer,line4 will cause R to point to the memory element with address 3 , which is irrelevant to the concept of alias .

Fortunately, this is not the case:R has not changed (pointing unchanged), but x has changed, in this use case, the alias interpretation of R is both grammatical and semantic than the pointer interpretation.

To sum up,reference is like a photon with "wave-particle duality", it has "pointer-alias" duality, and does not understand the three aspects of grammar-usage-semantics, and can not recognize reference the true meaning.

If only two elephants are available,oop (c + + supports oop) has an important concept, called polymorphism, which is that a "variable" can actually represent both "base class" and its derived class (derived Class): It can be either a or B (assuming a is the base class of B) (also a typical quantum paradox). This polymorphic variable can only be pointers and reference, and cannot be a " direct variable ". This feature adds another hat to reference :polymorphic variable, popular words, it is a "monster" with different identities at the same time. The details here will be discussed again next time.

Why polymorphic variable can only be pointers and reference ? I personally think that this can not be explained by formal logic and the use of the normal logical, only by the nature of the pointer and the C + + object Model memory structure to explain-this is a semantic level to justify the concept of C + + quantum characteristics of another corroboration. In the future I will use the article to interpret the major issue of C + +.

In short, a reference concept contains three completely different interpretations that can be popularly known as the Trinity :

    • Alias (same body name)
    • Pointer (pointer)
    • Polymorphic variable (multi-identity variable) (I do not like the multi-variant of this industry is commonly used in translation, think it is completely conceptual confusion, wrong, there is no " state ", only identity )

To this can be a summary, the purpose of this article is not albeit vocabulary, but an attempt to interpret the basic concepts of C + + from the logic, mathematics, so as to arouse your deep thinking about C + +.

The conclusion of this article is:

1) Some basic concepts of C + + are not clearly defined in formal logic and traditional mathematics. This can only be interpreted from the semantics (compile target code) and physical (the structure of the computer's memory).

2) C + + semantics (program hardware expression), is the key to understand C + +. Other languages, such as java,c#, JavaScript, and so on, are closer to mathematical logic and do not care too much about their "semantics", 95% of which can be understood.

3) do not understand C + + semantics , to the C + + programmer to deepen and improve, write high-quality, high-performance C + + programs, has not negligible negative impact.

Yang Yi: 2014-8-18 Seattle

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.