c++ operator bool()的使用
c++ operator bool()的使用
·
#include <cstdint>
#include <stdio.h>
struct ConnectionHandle {
const int id = -1;
operator bool() const { return id == -2; }
};
int main() {
struct ConnectionHandle a;
if (a) {
printf("1\n");
} else {
printf("2\n");
}
}
定义了 operator bool() 之后才可以使用在 if(a) 。
更多推荐
所有评论(0)