zoukankan      html  css  js  c++  java
  • 设计一个追踪类

    设计一个追踪类

    ——本文来自于《C++沉思录》中的例子。

             用C++设计思想制作一个追踪类,实现功能:

             1.基本的追踪

             2.追踪开关

             3.对于输出信息指定输出文件

             程序如下:

    // 追踪类
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    class MyTrace
    {
    private:
        bool  ok_;
        FILE* f_;
    
    public:
        MyTrace() : ok_(true), f_(stdout) {}
        MyTrace(FILE* const f) : ok_(true), f_(f) {}
    
        void Print(const string& msg)
        {
            if (ok_)
            {
                fprintf(f_, "%s", msg.c_str());
            }
        }
        
        void On()
        {
            ok_ = true;
        }
    
        void Off()
        {
            ok_ = false;
        }
    };
    
    int main()
    {
        MyTrace mt;
        mt.Print("Begin main()
    ");
        
        mt.Print("Test On()
    ");
    
        mt.Off();
    
        mt.Print("Test Off()
    ");
    
        mt.On();
    
        mt.Print("Test On() again
    ");
    
        // ...
    
        mt.Print("End main()
    ");
        return 0;
    }

             该程序比较简单,不多做解释。

  • 相关阅读:
    01月04日假期学习
    个人加分项
    12月16日总结
    12月15日总结
    12月13日总结
    01月01日假期学习
    01月02日假期学习
    12月14日总结
    12月17日总结
    01月05日假期学习
  • 原文地址:https://www.cnblogs.com/unixfy/p/3386938.html
Copyright © 2011-2022 走看看