数据类型

来源:互联网 发布:unity3d做的大型游戏 编辑:程序博客网 时间:2024/05/22 02:24

因为所有计算机上的数据仅仅是一个比特序列,我们使用的数据类型来告诉我们如何解释内存中的内容,在一些有意义的方式。 你已经看到的数据类型:整数的一个例子。 当我们声明一个变量为整数时,我们告诉计算机“的一块内存,这个变量的地址将被解释为一个整数”。

当你分配一个值的数据类型,计算机需要照顾的细节位数据类型的值转换成适当的顺序编码。 当你问你的价值,该方案“重组”你的电话号码从内存中的位的顺序。

还有许多其他的数据类型在C + +中除了整数,其中大部分我们不久将覆盖。 作为速记,我们通常是指一个变量的“数据类型”,因为它是“型”。

声明一个变量

在“C + +基本”部分,你已经学会了如何声明一个整型变量:

1
intnVarName; // int is the type, nVarName is the name of the variable

To declare variables of other data types, the idea is exactly the same:

1
type varName; // type is the type (eg. int), varName is the name of the variable

In the following example, we declare 5 different variables of 5 different types.

1
2
3
4
5
boolbValue;
charchValue;
intnValue;
floatfValue;
doubledValue;

It’s that simple. (Well, almost — there are a few things you can’t name your variables, which we’ll talk about in the next section)

You can also assign values to your variables upon declaration. When we assign values to a variable using the assignment operator (equals sign), it’s called an explicit assignment :

1
intnValue = 5; // explicit assignment

You can also assign values to variables using an implicit assignment :

1
intnValue(5); // implicit assignment

Even though implicit assignments look a lot like function calls, the compiler keeps track of which names are variables and which are functions so that they can be resolved properly.

Declaring multiple variables

It is possible to declare multiple variables of the same type in one statement by separating the names with a comma. The following 2 snippets of code are effectively the same:

1
intnValue1, nValue2;
1
2
intnValue1;
intnValue2;

You can also assign them values on the declaration line:

1
2
intnValue1 = 5, nValue2 = 6;
intnValue3(7), nValue4(8);

Which is effectively the same as:

1
2
3
4
intnValue1 = 5;
intnValue2 = 6;
intnValue3 = 7;
intnValue4 = 8;

There are three mistakes that new programmers tend to make when declaring multiple variables in the same statement.

The first mistake is declaring each variable as int (or whatever type it is) in sequence. This is not a bad mistake because the compiler will complain and ask you to fix it.

1
2
3
intnValue1, intnValue2; // wrong (compiler error)
 
intnValue1, nValue2; // correct

The second error is to try to declare two variables of different types on the same line, which is not allowed. Variables of different types must be declared in separate statements. This is also not a bad mistake because the compiler will complain and ask you to fix it.

1
2
3
4
5
6
7
intnValue1, doubledValue2; // wrong (compiler error)
 
intnValue1; doubledValue2; // correct (but not recommended)
 
// correct and recommended (easier to read)
intnValue1;
doubledValue2;

The last mistake is the dangerous case. In this case, the programmer mistakenly tries to initialize both variables by using one assignment statement:

1
2
3
intnValue1, nValue2 = 5; // wrong (nValue1 is uninitialized!)
 
intnValue1 = 5, nValue2 = 5; // correct

In the top statement, the nValue1 variable will be left uninitialized, and the compiler will NOT complain. This is a great way to have your program intermittently crash and produce sporadic results.

The best way to remember that this is wrong is consider the case of implicit initialization:

1
intnValue1, nValue2(5);

This makes it seem a little more clear that the value 5 is only being assigned to nValue2. The explicit assignment case is no different.

Where to declare variables

Older C compilers forced users to declare all of the variables in a function at the top of the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
intmain()
{
    // all variable up top
    intx;
    inty;
 
    // then code
    usingnamespace std;
    cout << "Enter a number: ";
    cin >> x;
 
    cout << "Enter another number: ";
    cin >> y;
 
    cout << "The sum is: " << x + y << endl;
    return0;
}

This style is now obsolete. C++ compilers do not require all variables to be declared at the top of a function. The proper C++ style is to declare variables when and where they are needed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
intmain()
{
    // then code
    usingnamespace std;
 
    cout << "Enter a number: ";
    intx; // we need x starting here.
    cin >> x;
 
    cout << "Enter another number: ";
    inty; // we don't need y until now
    cin >> y;
 
    cout << "The sum is: " << x + y << endl;
    return0;
}

This has quite a few advantages. First, variables that are declared only when needed are given context by the statements around them. If x were declared at the top of the function, we would have no idea what it was used for until we scanned the function and found where it was used. Declaring x amongst a bunch of input/output statements helps make it obvious that this variable is being used for input and/or output.

Second, declaring a variable only where it is needed tells us that this variable does not affect anything above it, making our program easier to understand and requiring less scrolling. Finally, it reduces the likelihood of inadvertently leaving a variable uninitialized, because we can declare and then immediately initialize it with the value we want it to have.

Rule: Declare variables where they are needed.


原创粉丝点击