Custom stream to method in C++?

后端 未结 5 525
星月不相逢
星月不相逢 2020-12-06 08:37

I\'m making a logger and I wish to have some kind of stream-like happenings going on, ideally doing CLogger << \"Testing, \" << 1 << \",2,3\\n\";

5条回答
  •  不知归路
    2020-12-06 09:07

    Check out operator <<, which is what STL's streams overload.

    class CLogger
    {
    public:
        CLogger& operator << (const std::string& _rhs)
        {
            // work with it here
            return *this;
        }; // eo operator <<
    }; // eo class CLogger
    

    EDIT:

    See this page that outlines how std::ostream overloads operator << for different types:

    http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/

提交回复
热议问题