I'm trying to make a class with conversion operators to string and char*, this makes ambiguous call error when trying to create a string. assume i have the following class:
class A { public: A(){}; operator const char*() const { return "a" }; operator const string() const { return "b" }; } and the following main programs:
int main() { A a; string b = string(a); return 0; } this causing ambiguous call error to basic_string(A&) also tried the following:
class A { public: A(){}; operator const char*() const { return "a" }; explicit operator const string() const { return "b" }; } int main() { A a; string b = static_cast<string>(a); return 0; } this causing the same error. I need to have both operators in my code, how can i make this work?
note: using string b = a works but I need the string(a) format to work aswell
note 2: not using explicit keywork makes operator= ambiguous
const stringis a pessimisation, because it prevents moving from the returned value. You should change it to just conver tostring.