Difficulty: Easy
Correct Answer: C++ does not allow the redefinition of a default parameter.
Explanation:
Introduction / Context:
Default arguments simplify function calls by supplying omitted values. Two frequent sources of confusion are whether the default can be a function call and whether a default can be specified again in another declaration. Clarifying these rules avoids ODR and ambiguity problems in headers and source files.
Given Data / Assumptions:
Concept / Approach:
C++ does allow a default argument to be any valid expression (subject to scope), including a function call such as int f(int x = get_default());
as long as the callee is declared beforehand. However, C++ forbids redefining the default for the same parameter in another declaration; you should place the default once (typically in a header) and omit it from later declarations/definitions. Thus, statement A is false, statement B is false, and statement D is the correct choice: redefinition of a default parameter is not allowed.
Step-by-Step Solution:
Verification / Alternative check:
Write two declarations: one with = get_default()
and a later one trying = 42
. The compiler rejects the second. Ensure get_default
is declared before the defaulted parameter; compilation then succeeds, proving A is false.
Why Other Options Are Wrong:
A: contradicts the language; defaults are ordinary expressions evaluated at the call site.
B: contradicts ODR-style rules for defaults.
C: a combination of two false statements.
Common Pitfalls:
Final Answer:
C++ does not allow the redefinition of a default parameter.
Discussion & Comments