原帖及讨论:http://bbs.bccn.net/thread-143314-1-1.html 在很多时候,我们都直接用stream来直接放在判断语句中。 其中的原因是:在判断语句中对自定义类型会调用operator void*()函数返回void*型的指针. 而在流的operator void*()中是这样定义的return (fail()?0:(void*)this); 所以在ifstream in("1.txt");assert(in);中,assert(in)其实就是assert(!in.fail());
所以要想我们自己定义的类型也可以像流一样直接方在判断语句中,我需要写两个操作符函数 struct Test { ... operator void*(); bool operator!(); ... }; 那么我们也可以 Test t; assert(t); if(t)cout<<"yes"<<endl; if(!t)cout<<"no"<<endl;  
说明:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
|