title: C++ overloading conversion operator for custom type to std::string
body: <p>I hope someone might be able to answer why the following doesn't work. Bear with me though, I am still very much a noob...
  I just cannot get to the bottom of why the following</p>
  
  <pre><code>using namespace std;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  class testClass
  {
  public:
   operator char* () {return (char*)"hi";};
   operator int ()  {return 77;};
   operator std::string  () {return "hello";};
  };
  
  int main()
  {
   char* c;
   int i;
   std::string s = "goodday";
  
   testClass t;
  
   c = t;
   i = t;
   s = t;
  
   cout&lt;&lt; "char: " &lt;&lt; c &lt;&lt; " int: " &lt;&lt; i &lt;&lt; " string: "&lt;&lt;s&lt;&lt;endl;
  
   return 0;
  }
  </code></pre>
  
  <p>gives me a compile time error:</p>
  
  <pre><code>myMain.cpp: In function ‘int main()’:
  myMain.cpp:23: error: ambiguous overload for ‘operator=’ in ‘s = t’
  /usr/include/c++/4.2.1/bits/basic_string.h:500: note: candidates are: std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp; std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::operator=(const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]
  /usr/include/c++/4.2.1/bits/basic_string.h:508: note:                 std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp; std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]
  /usr/include/c++/4.2.1/bits/basic_string.h:519: note:                 std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp; std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]
  </code></pre>
  
  <p>If I do not attempt the assignment </p>
  
  <pre><code>s = t;
  </code></pre>
  
  <p>it does work.</p>
  
  <p>I've been trying for hours to even comprehend the error message, but what's puzzling me most is that is does work for char*.</p>
  
  <p>I'm grateful for any hint.
  Thanks!
  Markus</p>