The variable of C + + check missing trap

Source: Internet
Author: User

What is a variable?

we often have int a=10 in our daily programming; this A is a variable. Variables provide a name-based store that the program can manipulate, and if we are directly manipulating registers and memory addresses in the assembly era, we now abstract the underlying registers and memory addresses, taking an alias, which can also manipulate the memory.

Each side of C + + has a specific type that determines the size and layout of the variable, the range of values that can be stored in that memory, and some of the actions that can be made on that variable. Variables are often referred to as "variables" or "objects" in C + +.


What is an object?

What is an object? In general, an object is an area in memory that has a type , that is, an lvalue expression is evaluated to produce an object.


Defining Objects

1. Variable initialization


In C + +, the initialization of variables is generally divided into direct initialization and replication initialization:

    • Direct initialization: Using the ' () ' number to achieve: int a (10);
    • Copy initialization: Use the ' = ' sign to achieve: int a = 10;

Note : initialization in C + + is not an assignment , first initialization is a value that creates a variable and assigns a value, whereas an assignment refers to a variable before it exists, except that it erases the value of the current object and assigns a new value.

For example, the following:

int a=10;//Copy initialization <span style= "Font-family:comic Sans MS;" >//+++++++++++++++++++++++</span><span style= "Font-family:comic Sans MS;" >int b;b=10;//assignment, not initialized </span>
Distinguishing between the two is especially important for C + + programming!


2. Initialization of multiple variables

Let's start by looking at a simple C + + assignment statement:

int number = number;

Is this statement correct or wrong?

When more than two variables are defined in a definition, each variable may have its own initialization, and the name of the object becomes visible immediately, so the following variables can be initialized with the variables previously defined in the same definition:

int number1=10,number2=number1;

So what if it's an int number = number? We said earlier that replication initialization is a two-step, the first step is to create a variable, the second step is to give the new value, we first created the variable number, then the number variable is immediately visible, and then the visible number to initialize themselves (although it makes no sense, Eventually, there is no initialization). So the compiler doesn't get an error.


initialization rules for variables

The initialization of variables can be divided into two types:

    • Initialization of built-in type variables: whether the built-in type variable is automatically initialized depends on where the variable is defined: 1. Variables defined in the function body are automatically initialized to 0. 2. The variable complement auto-initialization defined in the function body , in addition to being used as an lvalue to assign values, is undefined for other uses of the initialized variable. Note: It is recommended that the objects for each built-in type be initialized, although this is not required, but will increase the security of the program.
    • Initialization of class-type variables: If a class is defined without an initialization, the class can also automatically invoke the default constructor (without any initialization, then call the default constructor) to initialize it, such as the Std::string class, if there is no initialization (std::string STR;), then the default constructor is automatically called to initialize str as an empty string. Note: If the user does not have any constructors defined, the compiler will automatically generate a default constructor for you, which does not have any initialization effect, just to meet the needs of the compilation, if the user defines one or more constructors, Then the system will no longer automatically generate default constructors, so it is a good idea to customize a default constructor (the default constructor) when you customize the constructor. Reduces compilation errors When there are no initialization definitions (arrays, dynamic arrays, containers, as members of a class, and so on). (the default constructor is described in: http://blog.csdn.net/wuliming_sc/article/details/3855270)
Definition and Declaration

It is also important to distinguish between the definition of variables and the declaration of variables in subsequent programming.

Variable definition: Used to allocate memory space for a variable, and you can also specify an initial value for the variable. In a program, variables have only one definition

Declaration of a variable: used to indicate to the program the type and name of the variable. A definition is also a declaration: When defining a variable, we define its type and name. We can use the extern keyword to indicate that it is in the declaration and not in the definition.

Such as:

extern int A;//declaration int A;//definition
extern does not allocate storage space, it can only be explained that the variable is defined elsewhere in the program, such as in other C files:

<span style= "FONT-SIZE:18PX;" >//example.cint A=10;//++++++++++++++++++//main.cextern int A;main () {    int b=a;//b=10;} </span>
The result of the compilation is:

Gcc-c Main.c-o MAIN.O
Cc-c-O example.o example.c
GCC MAIN.O EXAMPLE.O

There is no problem.


If there is an initialization formula at the time of declaration, then it can be treated as a definition!

<span style= "Font-family:comic Sans ms;font-size:18px;" >//example.cint A=10;//++++++++++++++++++//main.cextern int a=11;//error, when linking two example.o and MAIN.O A has redefined main () {    int b=a;//b=10;} </span>
Gcc-c Main.c-o MAIN.O
Main.c:4:12:warning: ' A ' initialized and declared ' extern ' [enabled by default]
extern int a=11;
^
Cc-c-O example.o example.c
GCC MAIN.O EXAMPLE.O
EXAMPLE.O: (. data+0x0): ' A ' is defined multiple times
MAIN.O: (. data+0x0): For the first time in this definition
Collect2:error:ld returned 1 exit status
Make: * * [main] Error 1






The variable of C + + check missing trap

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.