I define several overloads of the assignment operator as follows:
foo.h
class Foo
{
private:
bool my_bool;
int my_int;
std::string my_string;
public:
Foo& operator= (bool value);
Foo& operator= (int value);
Foo& operator= (const std::string& value);
};
foo.cpp
Foo& Foo::operator= (bool value) {my_bool = value; return *this;}
Foo& Foo::operator= (int value) {my_int = value; return *this;}
Foo& Foo::operator= (const std::string& value) {my_string = value; return *this;}
And here is my main.cpp (see comment marked SURPRISE):
Foo boolFoo;
Foo intFoo;
Foo stringFoo;
boolFoo = true;
intFoo = 42;
stringFoo = "i_am_a_string";
std::string s = "i_am_a_string";
stringFoo = s;
stringFoo = static_cast<std::string>("i_am_a_string");
Question: Can someone tell me why the string literal is not passed in a boolean context?
source
share