ar进行信息输入和输入的一个细节问题

在使用ar<<和ar>>对数据进行输入输出时,CArchive::operator   >>   对bool型没重载,  必须换成BOOL 。我在一个程序里曾碰到这样的问题,声明一个bool型数组,然后想将其信息输入:

bool m_bColor[64];

...

  for(i=0; i<64; i++)
  {
   ar<<m_bColor[i]; //信息输入
  }

但编译后出现如下错误:error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'bool' (or there is no acceptable conversion)。

将bool换成BOOL问题得以解决。

那么,bool和BOOL到底有什么区别呢?在查MSDN时CArchive::operator <<时:

CArchive::operator <<

This operator stores the indicated object or primitive type to the archive.

friend CArchive& operator <<( CArchive& ar, const CObject* pOb );
throw( CArchiveException, CFileException );

CArchive& operator <<( BYTE by );
throw( CArchiveException, CFileException );

CArchive& operator <<( WORD w );
throw( CArchiveException, CFileException );

CArchive& operator <<( LONG l );
throw( CArchiveException, CFileException );

CArchive& operator <<( DWORD dw );
throw( CArchiveException, CFileException );

CArchive& operator <<( float f );
throw( CArchiveException, CFileException );

CArchive& operator <<( double d );
throw( CArchiveException, CFileException );

发现并没有单独对BOOL和bool类型变量重载。事实上,BOOL就是int 型。

bool在C++里是占用1字节,而BOOL是int类型,int类型的大小是视具体环境而定的;所以来说:false/true只占用1个字节,而TRUE/FALSE视具体环境而言,以下是BOOL在windef.h中的定义:typedef   int   BOOL;   
    
false/true是标准C++语言里新增的关键字,而FALSE/TRUE是通过#define,这要用途是解决程序在C与C++中环境的差异,以下是FALSE/TRUE在windef.h的定义:  
   
  #ifndef   FALSE  
  #define   FALSE   0  
  #endif  
   
  #ifndef   TRUE  
  #define   TRUE   1  
  #endif   
    
    
也就是说FALSE/TRUE是int类型,而false/true是bool类型;所以两者不一样的,只不过我们在使用中没有这种感觉,因为C++会帮你做隐式转换。   
    

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值