The code below prints 'operator bool' when used in the if statement and operator void* when it needs a bool for function call?
Why isn't it using operator bool for the function call too? and how to make it use it in both cases?
#include <iostream>
class A
{
public:
explicit operator bool() const
{
std::cout << "operator bool" << std::endl;
return true;
}
operator void*() const
{
std::cout << "operator void*" << std::endl;
return 0;
}
};
void f(bool valid)
{
std::cout << "f called " << valid << std::endl;
}
int main(int argc, char *argv[])
{
A a;
if(a)
std::cout << "if a" << std::endl;
f(a);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire