Practice mode C++

Assuming standard headers and using namespace std, what does this C++ code print? void f(int) { cout << “int”; } void f(char*) { cout << “ptr”; } int main() { f(nullptr); }

Correct answer D. ptr

Explanation

nullptr has type std::nullptr_t, which converts implicitly to any pointer type but not to int, so only f(char*) is viable and ptr is printed. With the old NULL macro (often defined as 0), the call could pick f(int) or be ambiguous. This is the main reason modern C++ prefers nullptr for null pointers.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.