Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
8.1 Mб
Скачать

3.7.5Initializing a Double-Precision Variable

You may have found out that when you declare and initialize a float or Single variable, the compiler generates a warning. Consider the following example:

using namespace System;

Int main()

{

float Fraction = 12.35;

Console::WriteLine(Fraction);

Console::WriteLine();

return 0;

}

When compiled, you would get the following warning:

.\Exercise.cpp(5) : warning C4305: 'initializing' : truncation from 'double' to 'float'

Although the program would compile fine. By default, when the compiler sees a value such as 12.35, for the sake of precision, it tends to store it as a double value. If you really want the variable to be treated as a float or Single, add the f or F to its right. Here is an example:

using namespace System;

Int main()

{

float Fraction = 12.35f;

Console::WriteLine(Fraction);

Console::WriteLine();

return 0;

}

This time, the compiler will not generate a warning. Remember that you can use f (lowercase) or F (uppercase).

3.8Variable Reference

3.8.1 The typedef Type Definition

The data types we have used so far may have names we are not familiar with. C++ allows you to customize the name of a data type to a name you are more familiar with. You would not (and are not allowed to) create a new data type, you would only "redefine" the name of an existing data type.

To customize the name of a data type, you can use the typedef keyword. The formula used is:

typedef KnownDataType NewName;

The typedef keyword is required to let the compiler know that you are creating a new data type. The data type must be one that exists already, such as those we have reviewed so far. It could be an int, an unsigned int, an Int32, a char, a double, etc. An example of using a typedef is:

using namespace System;

Int main()

{

typedef unsigned int PositiveNumber;

return 0;

}

In this case, PositiveNumber is just a new name for an unsigned int. It can be used as a new data type exactly as if you were using an unsigned int. Here are examples:

using namespace System;

Int main()

{

typedef unsigned int PositiveNumber;

typedef double Salary;

PositiveNumber Gender = 1;

Salary WeeklySalary = 1450.88;

Console::Write("Gender: ");

Console::WriteLine(Gender);

Console::Write("Salary: ");

Console::WriteLine(WeeklySalary);

return 0;

}

This would produce:

Gender: 1

Salary: 1450.88

3.9Native References

A reference is a variable that is a duplicate of an existing variable. It provides a technique of creating more than one name to designate the same variable. The syntax of creating or declaring a reference is:

DataType &ReferenceName = VariableName;

To declare a reference, type the variable’s name preceded by the same type as the variable it is referring to. Between the data type and the reference name, type the ampersand operator “&”. To specify what variable the reference is addressed to, use the assignment operator “=” followed by the name of the variable. The referred to variable must exist already. You cannot declare a reference as:

Int &Mine;

The compiler wants to know what variable you are referring to. Here is an example:

using namespace System;