关于类的简单运用——自制bool

#include<bits/stdc++.h>
#define TRUE ((_BOOL)1)
#define FALSE ((_BOOL)0)
struct _BOOL{
	char status;
	_BOOL(){
		status=0;
	}
	_BOOL(int x){
		if(x>0)status=1;
		else status=0;
	}
	_BOOL(const _BOOL &x){
		status=x.status;
	}
	bool operator=(int x){
		if(x>0)status=1;
		else status=0;
	}
	bool operator=(const _BOOL &x){
		status=x.status;
	}
	operator bool(){
		return status;
	}
};
int main(){
	_BOOL a=1;
	_BOOL b;
	b=0;
	if(a)printf("a\n");
	if(b)printf("b\n");
	return 0;
}

operator bool()这个函数表示这个结构体可以当做一个bool的类型来使用,这是最关键的一点。

posted @ 2022-04-16 15:19  计算机知识杂谈  阅读(35)  评论(0编辑  收藏  举报