Ask Your Question
0

What is the reason that function overloading fails with the presence of default parameters?

asked 2023-07-17 04:46:44 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-07-17 05:06:01 +0000

devzero gravatar image

Function overloading does not actually "fail" in the presence of default parameters. However, when a function is overloaded and some of the overloaded versions have default parameters, it can lead to ambiguity in some situations.

For example, consider the following two functions:

void foo(int a, int b = 0);
void foo(int a, int b);

When the compiler encounters a call to foo with two integer arguments, it will be ambiguous which of these functions to call. This is because both functions could be called with two arguments, and the first function could also be called with just one argument (since b has a default value).

To avoid this ambiguity, one of the functions could be renamed, or the default parameter could be removed.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-17 04:46:44 +0000

Seen: 16 times

Last updated: Jul 17 '23