operator overloading
suppose you have this snippet:
1 2 3 4 5
|
ifstream fin("test.txt");
if(fin) // this operator
{
// stuff
}
|
How do I overload the operator here? I know that you can overload the logical negation operator:
bool operator!() const;
But I want to know how to define the operator shown above, but have no idea what the prototype is.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <iostream>
int main()
{
struct A
{
explicit operator bool() const { return ( true ); }
};
if ( A() ) std::cout << "It works indeed!" << std::endl;
return 0;
}
|
perfect, thank you.
What are you trying to do?
Do you want this:
1 2 3 4 5 6
|
ifstream fin("test.txt");
if(fin.is_open()) // this operator
{
// stuff
}
|
Topic archived. No new replies allowed.