C++ default arguments: which statement below is incorrect regarding default function arguments?

Difficulty: Easy

Correct Answer: Default argument cannot be provided for pointers to functions.

Explanation:


Introduction / Context:
Default arguments in C++ let callers omit trailing parameters. They are inserted by the compiler at the call site. Understanding where defaults are allowed and the one-definition rules prevents ambiguous interfaces and linker surprises—especially in multi-declaration scenarios and for function-pointer parameters.


Given Data / Assumptions:

  • We consider regular functions and parameters that may be function pointers.
  • We allow all parameters to have defaults if desired.
  • We respect the rule: provide defaults once (typically in a header), not repeatedly.


Concept / Approach:

Default arguments may be supplied for any parameter, including a pointer-to-function parameter: e.g., void f(int, int (*pf)() = nullptr);. An entire parameter list may be defaulted, making the function callable with no arguments. However, a default argument must be specified only once for a given parameter across declarations; redeclaring a different default later is ill-formed. Therefore, the incorrect statement is the claim that “Default argument cannot be provided for pointers to functions.” It can be provided.


Step-by-Step Solution:

Evaluate A: allowed → correct statement. Evaluate B: allowed (all parameters may have defaults) → correct statement. Evaluate C: claims prohibition → incorrect. Evaluate D: redeclaration/redefinition of a default is not allowed → correct statement.


Verification / Alternative check:

Compile a sample where a function-pointer parameter has a default. It compiles. Add a second declaration that attempts to change the default and observe a compile-time error, confirming D and refuting C.


Why Other Options Are Wrong:

Labelled “wrong” here means “not the incorrect statement.” A, B, and D are all accurate rules of C++ defaults.


Common Pitfalls:

  • Placing defaults in multiple headers or both declaration and definition.
  • Forgetting that callers must omit only a suffix of arguments; you cannot skip a middle parameter.


Final Answer:

Default argument cannot be provided for pointers to functions.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion