Type conversion Learning notes in C + +

Source: Internet
Author: User

1, in the numerical calculation process some commonly used numerical conversion rules:

(1) If the two operand has one operand of type long double, then the other operand, regardless of type, is converted to a long double type.

(2) If the two operand has one operand of type double, and the other operand is not a long double type, the other operand is converted to a double type.

(3) If the type of the two operands is not a double (including a long double), and the type of one operand is float, the other operand is converted to the float type.

(4) If the two operands are integers, the compiler promotes all operands that are less than int to type int.

2, the form of explicit conversion:cast-name<type> (expression).

Type is the target type of the transformation and expression is the value to be converted. If type is a reference type, the result is an lvalue. Cast-name is one of the Static_cast,dynamic_cast,const_cast and reinterpret_cast.

3, Static conversion: static_cast.

Any explicitly defined type conversions can use static_cast as long as they do not contain the underlying const.

4. Dynamic conversion is also a mandatory type conversion method provided by the C + + language. Unlike static conversions, it is capable of type checking before conversion. Dynamic conversions are typically used to convert a class object pointer to another class object pointer. If the source pointer type is incompatible with the target pointer type, the result of the conversion is null. A program can determine whether the coercion type succeeds by detecting whether the result is null.

Use the dynamic_cast keyword for dynamic conversion in C + +. A dynamic conversion can only be converted to a void * (untyped pointer) type or a class object pointer, and the class must contain a virtual method and cannot convert to a normal data type.


5, constant conversion, used to convert a const object to a non-const object.

As below, you can convert to the same type of a very important pointer:

#include <iostream>using namespace Std;int main () {    const int max_a =;   Long *plen = (long*) &max_a;   cout << *plen << Endl;  In C, you can do    //in C + +:    int * Plen = Const_cast<int *> (&max_a);   Long *plen = const_cast (long *) (&max_a);  For different types of long type, the compilation does not pass    cout << *plen << Endl;    return 0;}
However, the following procedure, although the conversion succeeds, but the value after the change is still output is a constant value.

#include <iostream>using namespace Std;int main () {    const int max_a =;   Long *plen = (long*) &max_a;   cout << *plen << Endl;  In C, you can do    //in C + +:    int * Plen = Const_cast<int *> (&max_a);   Long *plen = const_cast (long *) (&max_a);    cout << *plen << Endl;    return 0;}
Because Max_a is a constant, it is not possible to change the constant value in principle, so the compiler encounters a constant at compile time, translating it directly into a constant without addressing.

6, re-interpret the conversion.

The re-interpretation transformation is an unsafe way to convert any pointer type to another pointer type. Should be used sparingly in the program.

#include <iostream>using namespace Std;int main () {    char *psztext = "mrkj";    unsigned int *paddr = reinterpret_cast<unsigned int *> (psztext);    char * pszcaption = reinterpret_cast<char*> (paddr);    return 0;}

When using the redo translation, the converted pointer is typically not used in accordance with its data type. Using Reinterpret_cast in a program is dangerous and unwise, so it is recommended that developers use it sparingly and with caution.

7. Numeric and String conversions

Functions are in the <stdlib.h> header file.

(1) Convert string to integer: int atoi (const char * string)

String: Represents the strings to be converted.

Return value: If the function executes successfully, the return value is a string-converted integer, and if the argument string cannot be converted to an integer, the return value is 0. If the argument string is converted, the return value is unknown.

#include <iostream> #include <stdlib.h>using namespace Std;int main () {    char *pszheight = "+";    Char *width = "$";    int nheight = atoi (pszheight);    int nwidth = atoi (width);    int area = nheight * nwidth;    cout << area << Endl;    return 0;}

(2) Convert string to real number double atof (const char * string);

String: strings to be converted

Return value: If the function executes successfully, the return value is the string converted real number, if the parameter Boone that converts to a real number, the return value is 0.0, the overflow is also unknown.

(3) integer converted to string char *itoa (int value,char *str,int radix)

Value: Represents the integer to be converted

Str: Represents a character pointer used to store the converted string of functions

Radix: denotes cardinality, Range 2 ... 35, usually 10, that is, a 10-in conversion.

Return value: The function returns a character pointer to the STR parameter.

#include <iostream> #include <stdlib.h>using namespace Std;int main () {    int ndays = 365;    Char psztext[128] = {0};    Itoa (ndays,psztext,10);    cout << psztext << Endl;    return 0;}

Do not know why my codeblocks have g++ or not support, in VC 6.0 can be run correctly.

(4) The real number is converted to a string char * FCVT (double value,int count, int *dec,int *sign);

Value: Represents the real number to be converted

Count: Represents the number of digits after the decimal point, which is precision.

Dec: is a shaping pointer that represents the symbol for the number, 0 is positive, and 1 is negative.

Return value: If the function executes successfully, the return value is a character pointer, otherwise null.

#include <iostream> #include <stdlib.h>using namespace Std;int main () {   double dbpi = 3141.5926;   int Ndec;   int nsign;   char * PSZTEXT=FCVT (dbpi,2,&ndec,&nsign);   cout << "string:" << psztext << Endl;   cout << "Number of digits after decimal point is 2" << Endl;   cout << "Location of decimal point" << ndec << Endl;   cout << "Number of symbols:" << nsign << Endl;   return 0;}









Type conversion Learning notes in C + +

Related Article

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.