Given the following code, why does the compiler not allow implicit conversion when building Bar ? That is, Foo is constructed, how was a constructed, which (should) be used to build the Bar ?
#include <string> class ImplicitlyConvertToChar { public: ImplicitlyConvertToChar(const char* a_char) : m_string(a_char) { } ImplicitlyConvertToChar(const char* a_char, size_t a_end) : m_string(a_char) { } template <typename T_String> ImplicitlyConvertToChar(T_String const& a_string) : m_string(a_string.begin()) { } operator char const * () const { return m_string; } const char* m_string; }; class Foo { public: Foo(const ImplicitlyConvertToChar& a_charLike) : m_string(a_charLike) { } const char* m_string; }; class Bar { public: Bar(const Foo& a_foo) : m_foo(a_foo) { } Foo m_foo; }; int main() { Foo a("this works"); Bar b("Why doesn't this?"); }
source share