C language forced conversion and memory

Source: Internet
Author: User

 

Take a look at the following Fun class, including constructors and forced type conversion functions.

 

 
1. # include <iostream> 2. # include <cstdio> 3. # include "fun. h "4. using namespace std; 5. int main () 6. {7. fun f; 8. cout <f <endl; // output 5 9. printf ("% d \ n", f); // output 3 10. printf ("$ d \ n", (int) f); // output 5 11. return 0 ;}
It is not hard to see that the <call forcibly converts the second parameter to an int, but the % d of printf only reads the first 32 bits of the following parameters and parses them into an int. If you want to use printf and call the forced conversion method of the class, you need to explicitly convert the type of the object.

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.