首頁 > 程式設計 > [C++] fstream 中的! operator …

[C++] fstream 中的! operator …

雖然已經學過,但是重新複習的時候,突然覺得fstream file ; if(!file) … 很神奇!就手癢做一個簡單版的。

http://gist.github.com/95100

/* Usage: To implement a object as fstream with operator ! and open()
* Author: Allen (allen501pc@hotmail.com)
* Date: 04/14/2009
* Output:
* object has been opened!
*/

#include
using namespace std;
class object
{
private:
bool isset;
bool notopen;
public:
object()
{
isset=false;
notopen=true; /* Set it is not opened! */
}
bool operator!(); /* ! operator for checking open or not */
void open()
{
notopen=false; /* set it is opened */
}
};
/* operator ! for checking that opened or not
* if open, return false
* Or, return true
*/
bool object::operator!()
{
return this->notopen;
}

int main(int argc,char * argv[])
{
object ob; /* take class object as ob */
ob.open(); /* open */
if(!ob)
{
cout << "object has not been opened! " << endl; } else cout << "object has been opened! " << endl; return 0; } [/sourcecode]

分類:程式設計 標籤:
  1. 仍無迴響。
  1. No trackbacks yet.

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

%d 位部落客按了讚: