2

I overloaded a function like the following:

void myClass::saveAsMat (bool complete){
  ...
}

void myClass::saveAsMat (std::string filename){
  ...
}

In my main.cpp when I call the function like this:

myObj.saveAsMat(true);

everthing works fine. But when I instead call the function like this:

myObj.saveAsMat("myName");

the function is not being executed at all. I expected a compile or runtime error but there is nothing like that. The program gets compiled and runs only that the function is ignored. (I checked this by placing some std::cout inside the function).

Then I found out that I can solve this by changing the argument passed to:

myObj.saveAsMat(string("myName"));

Why do I have to set the extra string() for the argument? Other functions that are not overloaded are working fine by simply passing "myName" for a "std::string" paramenter.

Kevin Katzke
  • 3,581
  • 3
  • 38
  • 47
  • 6
    Conversion from `char*` to `bool` is a standard conversion; conversion to `std::string` is a user-defined conversion. The former is preferred in overload resolution. `myObj.saveAsMat("myName");` should be calling `myObj.saveAsMat(true);` – Igor Tandetnik Aug 31 '16 at 13:56

0 Answers0