#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) 。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐