Type conversion in C ++ rather old, or rather a classic operation. In short and in Russian: Conversion type – it is considered an indication of a set of computer bytes integer or real. Or a pointer to a specific class.
At all, to understand the type conversion is necessary to consider the mechanism and structure variables. For example, PAC line integer multiple: short int, int, long int, not counting the unsigned. And computer places for them a different number of memory cells (bytes): 1, 2 and 4 respectively. Convert within these types of one class is called the standard conversion. at:
int a = 1;
short int b = 2;
b = a;
Take the next: take the two younger byte of a variable "a", simply transplanted in two bytes of variable «b». Since the value in and (unit) it is currently placed in these two bytes, the program will give the correct result, and C ++ will not be against such a conversion.
Why do I need to consider the size of the variable in the conversion?
What happens if you put in a variable "a" number 123456789? It is perfectly fit in 4 byte type int, but 2 byte type short int it no longer falls. too much. С , as expected, it just cuts the long number, taking only the tail of it, and put into a variable «b». We obtain course nonsense. And with a minus, if you bring it to the screen. Converting from the variable, which occupies a greater number of bytes in the memory will not be correct. If you convert the contrary from the short in long for example such problems will not, because the bytes in the variable recipient longer, and places data that stores short quite enough.
It is necessary to know. Teachers often catch students on this focus, especially in the training manual giving a similar pitfall with variable poorly compatible with each other in size.
As for the types of real conversion, It is engaged in a math coprocessor. There is no longer just the bytes distilled from cell to cell, and special instructions are executed, which convert the whole into a type of material with a mantissa and exponent, as it should be, and return value. This is not a conversion, it is converted. Anyway, It will deal with the processor.
int a = 123;
float b = 25.14;
a = b;
b = a;
cout << a << ' ' << b << endl;
In this example, the math co-processor converts the real to integer, and will drive to a variable, and then do the reverse operation: It will drive a whole in his special register (It called ST), and after he would get from a real integral part, equal to that, that in variable “and”.
If you want to convert from a real double (8 bytes) in float (4 bytes), here such problems, as a whole will not be a. Math coprocessor just round up to the real value, fits in 4 bytes, that does not lead to an incorrect value:
double b = 1234567891234565555.25;
a = b;
cout << a << '\n' << b;
The result will still be the number 1.234568e + 18, which means a 1.2-plus 18-degree. Another thing, what in “a” there will be rounded to the number of the 6th decimal place, instead 1.234567891234566e + 18. In general, inaccurate calculations it does not interfere, but it is recommended to use the program material of the same type, that did not happen here such rounding.
In addition to the type of transformation there is such a thing as the cast.
Visibility typecast can show such an example:
float b = 25.14;
a = (int)b;
cout << a << ' ' << b;
If the conversion – this decision C ++ (or rather compiler), in what type of result, it uses a type cast, rigidly specified by the programmer. In this example, the programmer tells the compiler, you need to convert it into a int, but not in short int. After all 25 and placed in int and short, that selects the compiler programmer may not know.
In everyday life such type conversions actually rare. And it's not necessary. The ideology of programming in a residential hints to avoid such frills, because they can be a rake (in the above example in the conversion of whole), therefore casts in C ++ should not be used everywhere.
:) Thank you for this article its author Stilet – Super Moderator programmers offline ProgrammersForum.
|Newsletter of programming:|