Overloading with default arguments: which statement is correct under C++ rules? Assume you avoid ambiguity by careful design.

Difficulty: Easy

Correct Answer: All arguments of an overloaded function can be default.

Explanation:


Introduction / Context:
Overloading and default arguments interact. While both features are legal together, careless combinations can create ambiguous calls. The language itself does not forbid defaults on overloads; it only requires that declarations be consistent and unambiguous at call sites.


Given Data / Assumptions:

  • Multiple overloads of the same function name may exist.
  • Any single function declaration can have multiple defaulted parameters, provided they are trailing.
  • We are choosing a statement that reflects possibility, not necessarily best practice.


Concept / Approach:
All parameters of a particular overload can be defaulted, making that overload callable with zero explicit arguments. There is no “at most one default” restriction in the standard. Claims that overloads cannot have defaults are false; many real-world APIs mix both. The key is to ensure that different declarations do not lead to ambiguous calls, especially when some overloads have defaults and others do not.


Step-by-Step Solution:
1) Legal example: void f(int a = 1, int b = 2); // callable as f(), f(3), f(3,4)2) Coexist with overload: void f(double); // distinct signature3) Avoid ambiguous pairs like void f(int); and void f(int a = 0); within the same scope.4) Conclusion: It is correct that all arguments of an overloaded function can be defaulted.


Verification / Alternative check:
Compile a set of overloads where one has fully defaulted parameters and another has a clearly different signature; calls resolve correctly.



Why Other Options Are Wrong:
“At most one default”: false; multiple defaults are allowed.“Cannot have default”: false; perfectly legal.“Overloaded more than once cannot have default”: also false.



Common Pitfalls:
Specifying defaults for the same parameter in multiple declarations or crafting overload sets where omitted arguments cause ambiguity.



Final Answer:
All arguments of an overloaded function can be default.

More Questions from Functions

Discussion & Comments

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