如果直接将类/结构体作为判断语句会报错:
#include<iostream>
using namespace std;
struct Test{
int x;
};
int main(int argc, char* argv[])
{
struct Test t = {3};
if(t){
print(t.x);
};
return 0;
}
因为t是Test类型而不是bool类型。
需要在类/结构体中加入重载语句:
#include<iostream>
using namespace std;
struct Test{
int x;
operator bool(){
return x> 5 ? true:false;
}
};
int main(int argc, char* argv[])
{
struct Test t = {3};
if(t){
print(t.x);
};
return 0;
}