Jose Alberto wrote:
Hi,
Is it possible a implicit cast from const char * to bool?
Yes.
With this program:
class Foo
{
public:
pru (string param){ cout <<"string"<<endl;}
pru (bool param){ cout <<"bool";<<endl}
};
int main()
{
pru * p1=new pru("string");
string pp = "string";
pru * p2=new pru(pp);
pru * p3=new pru(true);
}
I get this results:
bool
string
bool
So in the first new, when I pass a const char type the compiler is
making a implicit cast to bool. Anybody can give me an explanation?
You can write something like:
const char* p = whatever;
if (p)
do_something();
else
do_something_else();
But an if statement wants a bool, so the pointer p must be implicitly
convertible to bool. So p is converted to a bool with the value false
if it's a null pointer, or true otherwise.
So in your above example, both overloads are candidates for the call.
The bool overload is chosen because bool is a built-in type, while
string is a user-defined type, and conversions to built-in types are
always preferred.
You have to add another overload for const char*.