"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Does \"cout\" Print \"1\" When a Function Pointer is Used Without Parentheses?

Why Does \"cout\" Print \"1\" When a Function Pointer is Used Without Parentheses?

Published on 2024-11-07
Browse:503

Why Does \

Why Does "cout a function without calling it (not f() but f;). Print 1 Always?"

In this code, the code tries to "call" a function named pr without using parentheses. However, this is not actually calling the function. Instead, it is passing the function pointer to the cout function. When the function pointer is implicitly converted to a bool value, it is evaluated as true. Since true is equivalent to 1 in C , the output is always 1.

To clarify, the following lines from the provided code are not invoking the pr function:

pr;
cout 

To truly call the pr function, you would need to use parentheses like pr().

This behavior stems from the fact that function pointers are implicitly convertible to bool. In C 11, it is possible to overload the operator

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3