Document directory
- Strong and weak language
- Implicit conversion
- New Forced type conversion method
- C # Forced type conversion
Strong and weak language
Mainstream programming languages such as C ++, C, Java, and C # are strongly typed, while some scripting languages are weak, such as VBScript and JavaScript.
1. A simple understanding of a strong language is that when a variable is defined, a fixed type is specified and the memory block size is fixed. for example, there are int, double, and long types. once a variable is specified as a type, it cannot be used as another type. except for conversion
In C ++, we often see that int, long, and other numeric types seem to be common to each other. in fact, the compiler implicitly performs the next conversion according to the actual situation. however, if you assign the pointer type to the integer type, it does not conform to the common habit. Therefore, the compiler does not convert the pointer type and directly reports an error.
2. A weak type language can be simply understood as a class of int, float, and char. there is no concept of type at all. in the past, we used the variables X and Y in mathematics. It is equivalent to a representative symbol. like placeholders, it can represent any type. for example, in commit Crip, we simply add a var before any variable. for example, VAR Pos = 123; Pos =
"I change"; some scripting languages simply do not need to be specified using something like var, directly x = 3.14; y = "I am God "; the variable is left on the equal sign of the entire identifier.This is based on the principle that all data is expressed as 0101 in the memory. I want to explain how to translate these 01 statements so that you don't need to use your compiler to limit me. but in fact, the concept of no type does not work either, such as a = 1 + "3"; how to identify 3 as a number or a string. therefore, the interpreter behind the weak type language will definitely make some class judgments and make some conversions. we can't see it anymore.
Weak languages are flexible and convenient to use. however, if there are many codes, it is certainly prone to errors. although the strong type is not that flexible, the type is safe. error-prone. in addition, the compiler will also perform a type check and report errors to you when some types are improperly used during compilation.
The weak type language does not differentiate so many types at all. so there is no type conversion. in order not to completely lose the flexibility of a weak language, a strong language also allows some conversions between different types. sometimes it is implicitly converted. however, it is best to force the conversion as much as possible.
Implicit conversion
In implicit type conversion, the condition for C ++ is a little loose. Almost all numeric types can be implicitly converted. for example, Int, float, and char can be converted to each other without any overflow or loss of precision. C # has many strict implicit conversion conditions. an error is reported only when no value overflows and no precision loss occurs.
For example, double dd = 12.12;
Int num = dd; // implicitly converts a dd to the int type.
This can be converted normally in C ++. However, in C #, the error. C # Must be converted explicitly.
Forced type conversion (explicit conversion)
Forced type conversion and explicit type conversion are just a few minutes. For text games, they are different.
Forced conversion of legacy types
The type conversion method in C language is very simple. For example, converting float to int is like this.
Double dd = 12.12;
Int num = (INT) dd; // or Int num = int (dd );
Add a type name before a variable and enclose it in parentheses.
C ++ continues to be compatible with C usage. but C # Only follows the previous one of the above two usage, that is, int num = (INT) dd; but int num = int (dd) is not used any more, if you use the compiler like this, an error will be reported.
The old style is simple and convenient, but it also has some disadvantages. For example, if there are many parentheses in a series of expressions, it is not easy to see that it is a type conversion, and the visibility is poor. there is no error handling mechanism. therefore, both C ++ and C # have created a new conversion method.
New Forced type conversion method
C ++ has four conversion methods: static_cast, dynamic_cast, const_cast, and reinterpret_cast.
You may see so many keywords for the first time, and it looks ugly. It is certainly not very pleasing to the eye. You may think it is much easier to add parentheses in the C language. the four conversion methods of C ++ have a saying (of course I'm not sure about the authenticity) that is actually intentionally made so ugly and so many of them, the purpose is to make it inconvenient for you to use it. this is because the reason why a strong-type language generates so many types is to let you use them according to the rules. conversion is performed unless otherwise unavailable.
1. const_cast
Only one function of this conversion method is to convert the const type to a non-const type. This conversion method can only be used, and errors will occur if the other three methods are used as officers.
You may be confused. The purpose of our definition of const is to avoid modifying the variable and how to convert it into non-Const. most of the time, we naturally don't have to worry about making such a conversion, which is only used in some special cases. for example, you have the pointer const char * Pc = "Arwen ";
But there is a function that requires you to convert the char * parameter into. For example, fun (char * PC), so you can convert it to a non-Const.
Fun (const_cast <char *> (PC ));
Of course, you actually need to use the old-fashioned method to achieve the same purpose fun (char *) PC );
For example
Int num = 44;
Const int * pnum = & num;
// * Pnum = 88; an error is returned.
// Int * IP = pnum; this does not work.
Int * IP = const_cast <int *> (pnum); // OK, equivalent to int * IP = (int *) pnum;
* IP = 88;
Cout <num; // The result is 88.
2. reinterpret_cast
It is generally used to convert different types of pointers, or to convert between pointers and values. For example
Int num = 123;
Int * pnum = & num;
Char * pchar = "weiwen ";
Pnum = reinterpret_cast <int *> (pchar); // converts a char pointer to an int *.
Int NO = reinterpret_cast <int> (pchar); // converts a pointer to an integer.
Char * Pc = reinterpret_cast <char *> (NO); // convert a number to a char * pointer
Of course, all the conversions above can also be converted using the old method such as (int *) pchar, (INT) pchar, simply using parentheses. It is equivalent.
3. dynamic_cast
It is generally used for conversions between inheritance classes. If the class son inherits from father;
Fun (father * Fa)
{
Son * So = dynamic_cast <son *> (FA );
}
1) if there is no virtual function in Father, the above Code will report an error during compilation. if such a scenario is used, you can only use son * So = (son *) fa; // No error will be reported.
2) Of course, if father and son do not have an inheritance relationship, the above conversion result is so to get a null pointer. if son * So = (son *) fa is used, the conversion can be successful even if there is no inheritance relationship, but the pointer obtained in this way is certainly prone to many errors if it is used.
4. static_cast
This method is the simplest and easiest to understand, because it is actually called during implicit conversion. therefore, it is generally considered a reasonable conversion, such as conversion between various values. for example, if you want to convert the char * type to int type, an error will occur. it can also be replaced by an old-fashioned method.
In addition, if son inherits from father and father does not have a virtual function, dynamic_cast cannot be used, but static_cast can be used, for example, son * So = static_cast <son *> (FA );
C # Forced type conversion
C # has a keyword as. If the conversion fails, null is returned. This is mainly used for custom type conversion between various objects.
For example, son so = fa as son;
In addition, the built-in Conversion Function convert is generally used.
For example, a float type ff. is converted to an int type.
Int num = convert. toint32 (ff );
If the string type is converted to the numeric type, the available function is parse.
For example, int num = int. parse ("123 ");