Strange unsigned subtraction -- The rtti, typeid (type). Name

Source: Internet
Author: User

2011-03-29 wcdj

 

// Test code <br/> # include <stdio. h> <br/> int main () <br/> {<br/> unsigned short US1 = 2, us2 = 3; <br/> int I1 = (US1-us2> 0); <br/> unsigned int ui1 = 2, ui2 = 3; <br/> int I2 = (ui1-ui2> 0); <br/> printf ("% d, % d/N", i1, I2 ); // 0 1 <br/> return 0; <br/>}< br/>

 

Explanation:
Unsigned short increases the type to int, so the us1-us2 is not an unsigned number, but an int type number, that is, a signed number. You can use the following code for verification:

# Include <iostream> <br/> using namespace STD; <br/> int main (INT argc, char * argv []) <br/>{< br/> unsigned short US1 = 2, us2 = 3; <br/> cout <(US1-us2) <Endl; // output-1 <br/> cout <typeid (US1-us2 )). name () <Endl; // type upgraded to int <br/> int I1 = (US1-us2)> 0 ); <br/> cout <I1 <Endl; // 0 <br/> unsigned int ui1 = 2, ui2 = 3; <br/> cout <(ui1-ui2) <Endl; // output a large int 4294967295 <br/> cout <typeid (ui1-ui2 )). name () <Endl; // The type is still unsigned int <br/> int I2 = (ui1-ui2)> 0 ); <br/> cout <I2 <Endl; // 1 <br/> return 0; <br/>}< br/>

 


Knowledge supplement:

Rtti(Run-Time type identification,Runtime Type Recognition), Which enables the program to obtain the actual derived type of the object pointed to by the base pointer or reference, that is, the program that allows the operation of objects by pointer or reference to the base class can obtain the actual derived type of the pointer or reference to the objects. In C ++, two operators are provided to support rtti:Dynamic_castAnd
Typeid.

Typeid is one of the keywords of C ++, which is equivalent to operators such as sizeof. The typeid operator returns a reference to an object of the standard library type named type_info (defined in the header file typeinfo). Its expressions includeTwo Forms:( 1) typeid (type) (2) typeid (expr)

Note:
If the expression type is a class type and contains at least one virtual function, the typeid operator returns the dynamic type of the expression, which must be calculated at runtime. Otherwise, the typeid operator returns the static type of the expression, it can be computed during compilation.

The iso c ++ standard does not have the exact definition of type_info. Its exact definition is related to the compiler, but the standard sets out that its implementation must provide the followingFour operations:
(1) T1 = t2 if the two objects t1 and t2 are of the same type, true is returned; otherwise, false is returned.
(2) T1! = T2 true is returned if the T1 and T2 objects are of different types; otherwise, false is returned.
(3) T. Name () returns the C-style string of the type. The type name is generated using system-related methods.
(4) t1.before (T2) returns the bool value indicating whether T1 exists before T2.

Note:
The name member function of type_info returns the C-style string to indicate the corresponding type name, however, it is important to note that the returned type name is not necessarily consistent with the corresponding type name used in the Program (this is often the case), which is determined by the implementation of the compiler, the standard only requires that a unique string be returned for each type.

Refer:
Http://www.cppblog.com/smagle/archive/2010/05/14/115286.html
Http://topic.csdn.net/u/20110328/18/566db861-e4ac-42a6-82aa-10eb41d0c547.html

 

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.