Assigning cout to a variable name

后端 未结 7 1545
旧时难觅i
旧时难觅i 2020-12-14 20:26

In ANSI C++, how can I assign the cout stream to a variable name? What I want to do is, if the user has specified an output file name, I send output there, otherwise, send i

7条回答
  •  孤街浪徒
    2020-12-14 20:56

    This took about two hours to get. Basically, I have a external class running a test suite. I send in a delegate to run the tests, so in order to have access to output I need to send in an output stream. I guess I could have done a different stream per test. Anyways, I wanted to pass in the ofstream to be used later.

    // Main code to create Test Suite Object
    ofstream debugFile("debug.txt");
    TestSuiteObject* myTestSuite = new TestSuiteObject(&debugFile);
    
    // Test Suite Object
    class TestSuiteObject: public Test::Suite
    {
    public:
     TestSuiteObject(std::ofstream* debug) : m_debug(*debug)
     {
      m_debug << "some witty remark" << std::endl;
      TEST_ADD(TestSuiteObject::test1);
      TEST_ADD(TestSuiteObject::test2);
      TEST_ADD(TestSuiteObject::test3);
    
     }
    
     void test1();
     void test2();
     void test3();
    
    private:
     std::ofstream& m_debug;
    };
    

提交回复
热议问题