2nd. Variables and Basic types 1---The difference between a variable and an object

Source: Internet
Author: User

In C + + primer: An object is a piece of memory that can store data and have some kind of space. The named object-----is called a variable (variable).

-------------------------------------------------------------------------------------------------------

Objects and variables These two concepts do not want to mess up, the neck is the neck, the vertebral is the vertebral, although both correspond to a piece of memory, but from different angles to understand the memory will have different meanings.

    1. Variables : The so-called variable is a definition, by defining the compiler will open up a space, and the memory space and the name of the variable bundled together . From this meaning to understand, in fact, the truth is: variable name is inside the code to visualize a symbol. That is, if an int a,a is just a symbol in the memory where an address is visualized in the code, a itself as a symbol does not occupy space, the space occupied by a is the corresponding variable, through a This symbol we can refer to the variable in memory position. Just like the relationship between a person and a name, a person as a real entity, is to occupy the physical space of the Earth, is a real existence, and this person's name just represents a symbol of this person, itself does not occupy the physical space of the Earth, but when his mother gave him birth, To bind the entity and name together, so that when we quote the person's name, we find this person.
    2. Object : An object is a type of area in memory . From the description of this sentence, the object as to the variable, seems to be more concerned about the type of memory, rather than the name. You cannot say that a variable is an object, or an object is a variable, strictly speaking, an object is used to describe a variable. One of the proofs is that the concept of a temporary variable in C + +, such as a temporary variable generated at the time of the argument, exists in memory, but is not named, and therefore cannot be visualized in the code, we cannot refer to the temporary variable by its name.
the difference and connection between the twoAs we can see from the two descriptions above, both are used to describe a memory, but they are described from different angles:
    • variables are more emphasis on the meaning of this symbol of the variable name, more emphasis on the relationship between the name and memory, without having to pay attention to what type of memory is , how many bytes in length, only focus on the variable name a corresponds to a certain memory.
    • The object's description is more about the type of memory than the name, that is, from the object's perspective to see the memory, you need to know the length of the memory bytes and other information, and do not care whether the object in the code has a variable name to refer to this memory.

-----------------------------------------

Summary-The object is concerned with memory space, whereas variables are concerned with the relationship between the name and memory.

Example: int A; If we say a is a variable, then we are concerned that a is the name of a memory, when we refer to a, we can clearly know that we are referring to the name of a the corresponding memory space, and not anything else, Also don't pay attention to this A's memory is an int ah char ah what kind of type. If we say that a is an object, then we need to know what type this object is, and when we reference and manipulate a, we can do something that conforms to the type semantics based on the type information, rather than brute-force access to the memory, arbitrary parsing of the data in memory.

2nd. Variables and Basic types 1---The difference between a variable and an object

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.