0

Is it possible to overload conversion operator from int to std::string? Is it possible to use this function:

void printToLog(std::string text);

Like this:

int a = 5;
printToLog(a);

Or this:

int a = 5;
printToLog(a + " bombs have been planted.");

?

lollypap54
  • 41
  • 7

1 Answers1

0

There's a function for it in the standard library. The aptly named std::to_string:

int a = 5;
printToLog(std::to_string(a) + " bombs have been planted.");

You cannot add a conversion operator for this purpose. Such an operator can only be a member of a class type, and int is not a class type. Neither can you add a converting constructor to std::string. Use the mechanism the standard gives you.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458