Larold
Larold

Reputation: 670

C++ specific syntax for adding default argument

Imagine you have a function X() all over your codebase. It takes three arguments. The prototype looks something like this:

In header file:

void X(string a, int b, string c);

In .cpp file:

void myclass::X(string a, int b, string c) {
...
}

Suppose you are adding to the codebase, and have a particular need to pass a 4th argument to X in only one spot. The 3-argument calls all need to remain in place. You would like to avoid breaking all of the 3-argument versions of the calls to X.

I have tried using the advice here but this doesn't work. Answer one still produces errors of no matching function found. Trying to do what answer 2 suggestions yields errors that I can't overload the function. Specifically, I try this, based on answer 2 in that link:

In header file:

void X(string a, int b, string c, string d);    
void X(string a, int b, string c, string d="default_value");

Any suggestions on how to correctly add a single 4-argument call without breaking all the 3-argument calls is much appreciated - thanks!

Upvotes: 0

Views: 918

Answers (4)

Luchian Grigore
Luchian Grigore

Reputation: 258608

Just add a new prototype, leaving the previous one unchanged:

string X(string a, int b, string c);    
string X(string a, int b, string c, string d);

You can't assign a default value to the new argument, since that would cause an ambiguity: the compiler can't possibly know which of the methods you want to call.

Or you could replace the old definition with a new one, taking one extra parameter, and set a default for that. But in this case, you have to remove the previous function declaration.

HINT:

Per your syntax, I think you're using

using namespace std;

in your header. This is bad practice. Doing so populates the global namespace with the std namespace wherever you include this header. You should remove this directive and instead qualify your types: std::string instead of string

Upvotes: 3

BЈовић
BЈовић

Reputation: 64223

This example compiles with maximum warning level:

#include <iostream>
#include <string>

void foo(std::string , int , std::string , std::string d="default_value")
{
    std::cout<<"d="<<d<<std::endl;
}


int main() {
    foo("param1",55,"param2");
}

The last parameter to the foo function has a default value.

Upvotes: 0

dlants
dlants

Reputation: 761

You only need the second definition in the header file. Having both of them is what's creating the overload error.

void string X(string a, int b, string c, string d="default_value");

Means, when 4 values are given, use the provided d, and when only 3 are given, set d to "default_value".

Upvotes: 0

Ajai
Ajai

Reputation: 3490

In header file you need to have two functions

void string X(string a, int b, string c);
void string X(string a, int b, string c, string d);

This way all the code segment that calls X with three arguments don't break and the single place where you are calling X with four arguments gets called (i.e., appropriate overloaded function gets called).

Upvotes: 1

Related Questions