觀念連結: http://dreamisadream97.pixnet.net/blog/post/284901036-Conversion%20Operators

 

#include <cstdio>

class myFile
{
    public:
        void open(const char _filename[])
        {
            good = ( (fd = fopen(_filename, "r")) != 0 );
        }

        explicit operator bool() const { return good; }

    private:
        FILE *fd = 0;
        bool good = false;
};

int main()
{
    myFile mf;
    mf.open("flag.txt");

    if(!mf) 
    {
        fprintf(stdout, "cannot open file\n");
    }
    else
        fprintf(stdout, "success\n");

    return 0;
}

文章標籤
全站熱搜
創作者介紹
創作者 大神(偽) 的頭像
大神(偽)

大神的世界

大神(偽) 發表在 痞客邦 留言(0) 人氣(9)