C++ 31 Jan 2010 23:35:20

Snippet: Custom Type to Output Stream

http://tinodidriksen.com/uploads/code/cpp/custom-to-ostream.cpp

#include <ostream>
#include <iostream>
#include <string>

struct Custom {
    std::string m_str;
    friend std::ostream& operator<<(std::ostream& stm,
        const Custom& obj);
};

std::ostream& operator<<(std::ostream& stm, const Custom& obj) {
    stm << obj.m_str;
    return stm;
}

int main() {
    Custom cst;
    cst.m_str = "waffles";
    std::cout << cst << std::endl;
}
StumbleUponFacebookDiggDeliciousRedditMySpaceShare

Subscribe to the comments through RSS Feed

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word