Kookehs
Kookehs

Reputation: 84

Workaround for ambiguous call to overloaded function

void printOutput(std::string text);
void printOutput(std::string& text);

Both functions print some text out to the console, but I wanted to handle each case where:

std::string testOutput = "asdf";
output->printOutput(testOutput); // Gives the error as it can use either function

In some cases I may want to:

output->printOutput("asdf"); // Only the first function can be used

Rather new to all this, is there a way I can handle this?

Upvotes: 0

Views: 124

Answers (2)

Qaz
Qaz

Reputation: 61970

Pass by const reference:

void printOutput(const std::string &text);

Both forms can bind to that, and you shouldn't have to modify what you print.

Upvotes: 2

Useless
Useless

Reputation: 67822

Unless you're planning to modify the string passed in by reference, a single

void printOutput(std::string const& text);

will work.

Or are you hoping to do something different in each version?

Upvotes: 1

Related Questions