2011-08-22 41 views
1

我有一個類從ostream繼承,但操縱器無法編譯。例如:C++ OStream繼承和機械手

QLogger &logger = QLogger::getInstance(); 

logger << hex << 10 << endl; 

抱怨約logger << hex。如果我將記錄器投到ostream它會起作用,但這是相當不利的。

如何擴展我的班級,使其具有操作員的行爲ostream

感謝,

肯尼

class LIBEXPORT QLogger: public std::ostream 
{ 
    friend class boost::thread_specific_ptr<QLogger>; 

    friend class QLoggerFunnel; 

public: 

    // Priority for message filtering. 
    // 
    // DEBUG Detailed information that may be useful during development or troubleshooting. 
    // NORMAL Message will be useful during normal operation. 
    // WARNING Message indicates an unhealthy condition but the system should continue to operate. 
    // FAULT Message indicates a critical error. The system may not operate normally. 
    enum Priority 
    { 
     DEBUG, NORMAL, WARNING, FAULT 
    }; 

    // DEPRECATED: Defined for backward compatibility. 
    static const Priority LOW = NORMAL; 

    static const Priority HIGH = WARNING; 

    static const Priority URGENT = FAULT; 

    // Returns a reference to the global instance. 
    static QLogger& getInstance(); 

    // DEPRECATED: Use instead: QLogger::instance() << "Example message." << std::endl 
    static AWI_DEPRECATED QLogger& stream(QLogger::Priority priority = NORMAL); 

    QLogger &operator<<(Priority p); 

    // Messages with a priority below the threshold are discarded. 
    void setThreshold(Priority priority); 

    Priority getThreshold(void) const; 

    // Set the priority of messages. 
    void setPriority(Priority priority); 

    Priority getPriority(void) const; 

protected: 

    static void handleThreadExit(QLogger *logger); 

    QLogger(); 

    QLogger(const QLogger&); 

    virtual ~QLogger(); 

    QLogger& operator=(const QLogger&); 

    void write(std::ostream &destination); 

    void synchronize(void); 

    // Prepends information to each line of its associated output stream. 
    class StampBuffer: public std::stringbuf 
    { 
     public: 

      StampBuffer(); 

      virtual ~StampBuffer(); 

      // String to be displayed before each line. 
      void setPreamble(const char *p); 

      virtual int sync(); 

      void write(std::ostream &destination); 

     protected: 

      boost::mutex _mutex; 

      std::stringstream _stream1; 

      std::stringstream _stream2; 

      // Active stream being written to. 
      std::stringstream *_write; 

      std::stringstream *_read; 

      const char *_preamble; 
    }; 

    class NullBuffer: public std::stringbuf 
    { 
     public: 

      NullBuffer(); 

      virtual ~NullBuffer(); 

      virtual int overflow(int c) 
      { 
       return (c); 
      } 
    }; 

    StampBuffer _buffer; 

    NullBuffer _null_buffer; 

    Priority _threshold; 

    Priority _priority; 
}; 
+2

這可能有助於查看「記錄器」的聲明。 –

+0

對不起。我應該格式化該代碼。 – Kenny

+0

我添加了聲明。它包括我使用納瓦茲的建議的實驗。 – Kenny

回答

1

如果您實現一個成員函數,則繼承了相同功能的重載重載決策不考慮。如果您將自定義的重載更改爲好友全局函數而不是成員,則應該可以正常工作。演員強制編譯從ostream中定義的那些中選擇operator<<()

函數指針的特殊重載是不需要的。

+0

你有沒有想過我爲什麼要做這些?如果我的課是一個ostream,它爲什麼不表現爲一個?我會嘗試你的建議,但我想知道我的班級不會做什麼。 – Kenny

+1

派生類型中聲明的重載隱藏了基類中的名稱。你也應該可以使用'使用std :: ostream :: operator <<':http://ideone.com/6ImOp – UncleBens

+0

等一下。你是否這樣說?是因爲我定義了運算符<<(優先級p),其他運算符<<未被考慮?哎喲!對不起,我在考慮將評論引導至基於已被刪除的建議添加的第二個重載。 – Kenny