How to define a static operator<<?

Posted by Pietro M on Stack Overflow See other posts from Stack Overflow or by Pietro M
Published on 2011-11-23T17:28:44Z Indexed on 2011/11/23 17:51 UTC
Read the original article Hit count: 240

Filed under:
|
|
|

Is it possible to define a static insertion operator which operates on the static members of a class only? Something like:

class MyClass
{
public:
    static std::string msg;

    static MyClass& operator<< (const std::string& token) {
        msg.append(token);
        return *this;   // error, static
    }
};

alternatively:

static MyClass& operator<< (MyClass&, const std::string &token)
{
    MyClass::msg.append(token);
    return ?;
}

This is how I would like to use it:

MyClass << "message1" << "message2";

Thank you!

© Stack Overflow or respective owner

Related posts about c++

Related posts about static