The "total" of C + + variables (mereology)

Source: Internet
Author: User

Stroustrup In his autobiography that he hated Hegel because of his philosophical influence on Kierkegaard (Gaul). So reading Stroustrup 's book seldom feels the importance of abstract theory. This also affects the culture of C + +: Many C + + programmers can write code, but the concept is unclear, feeling life difficult (:). This is more or less strange C + + 's originator Stroustrup himself.

Less gossip, the problem of the properties of C + + variables is the fundamental problem of C + +. Yet I have not read a book that can be made clear from abstract, theoretical heights. Today I'm trying to talk about the properties of C + + variables. I think that each C + + variable has six attributes that cannot be detached (merging names and classes ), and you are in the C + + gate:

Data type and name

There is no "unknown class" variable in C + +

C + + variables are identified by the uniqueness of the name, with no variable of the same name in the same scope

Storage type (storage Class)

Data segment Variable

Stacks (stack) variables

Stacked (heap) variable: (can only be a sub-variable)

Scope of application (scope):

1. Default name space (:: namespace)

2. Namespaces (named namespace)

3. Nameless Space (anonymous namespace): File variable

4. Type (static class variable or type variable)

5. Example (instance class member variable)

6. Inside function-static (static variable in-a function)

7. Within function (or within code block)-Dynamic (auto variable or stack variable)

Life cycle

Program start to finish: Global

Code block: Auto

New/delete:free

First use to program end: Static in function

Content:

Direct content

Direct variables

Indirect content

Pointer variable

Reference variable

Total Score (mereology)

Total (whole variable) variable

(part variable) variable: that is, the member variable

The first four properties all have writings, and the last of the six attributes seems to have been raised by no one or a fresh person.

On "Total Score", this is the only better translation I found when I looked for mereology translations. I can hardly find any information that has been discussed by previous generations (please tell me if you know).

He means "overall" and "part" of the relationship. Translation into the headquarters is not good, the total score is not perfect, but slightly more modest than the headquarters point:)

With this concept, we can further differentiate between the two types of variables:

A Independent variable: is a total, not a variable, or can be called a "parent variable"

B Member variable: is a partial body, which is subordinate to a general variable, or can be called a "sub-variable"

Why is the total score important for a variable? Because:

1) A variable that stores a Heap can only be a child variable.

A * PA = new A; Line1

The PA is a pointer variable, which itself is not in the heap, it just "points to the heap address".

If a is defined as : Class A {b b;}, then line1 causes the storage property of the child variable b to be the Heap .

From another perspective, the parent variable in the heap is always nameless , and it exists only indirectly through pointers.

2) The stored property of the child variable is determined by the storage property of the parent variable

such as class A {}; class B {A *pa;} ; Foo () {b b;}

B as a parent variable, the stored property is the stack, and its child variable PA, also in the stack. If b *b = new B, then the PA property is the Heap.

3) The sub-variable can be optimized to " domain " of its parent variable at run time, so its read/write is carried out through its parent variable, without "fetch" (that is, by name). Performance is slightly better than the parent variable.

For example, B.F1 can be optimized for b+offsetof (F1), and F1 is "erased by name" at run time.

Anyway

Understanding the six main attributes of C + + variables is an important part of C + + semantics in-depth understanding of C + + variables. Because the concept of "total score" is not emphasized by the experts, the special blankly is specially ordered here in order to give a tip.

The "total" of C + + variables (mereology)

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.