Why can't I implicitly construct an object given a suitable constructor when passing to an argument?

Viewed 842

In the below example, why can't I simply pass a string to the printFoo()?

#include <string>
#include <iostream>

using namespace std;

class Foo {
public:
  Foo(const Foo &foo) : str(foo.str) {}
  Foo(string str) : str(str) {}

  string str;
};

void printFoo(Foo foo) {
  cout << foo.str << endl;
}

int main() {
  Foo foo("qux");
  printFoo(foo); // OK

  printFoo("qix"); // error: no matching function for call to 'printFoo'

  return 0;
}

For whatever reason, I had in my head that a constructor would automatically be determined and used in order to construct an object.

Why can't I do this, but I can pass a char[n] constant to an argument accepting a std::string, for example?

4 Answers
Related