Archived

This topic is now archived and is closed to further replies.

Craazer

Cout style output

Recommended Posts

Hi can you help me and say what do I need to create my own ''cout class'' ? Writing to console isnt a problem but what comes to << operator im a bit lost. You see I can redefine operator << and use it like this: mycout << "test"; but not like this: mycout << "te" << "st"; And not to mention the endl defination, wich is probably: #define endl "\n" ?

Share this post


Link to post
Share on other sites
Your insertion operator (the formal name for our friend Mr. << ) needs to return a reference to the class so you can "chain" them together.


        
CMyClass& CMyClass::operator<<(const string& str)
{
/* Do something with the string */
}


And you shouldn't use std::endl with your custom class, it's not defined as trivially as you think.

However, if you're creating your own stream class, you just might. I'm unsure whether you literally meant a cout-derived class, or just a class that uses an insertion operator, hence the quotes around "cout class."

[edited by - Zipster on May 3, 2003 3:18:12 PM]

Share this post


Link to post
Share on other sites
The insertion operator should be defined as a binary operator outside the scope of the class, like so:


    

inline std::ostream& operator <<(std::ostream& stream, const YourClass& r)
{
// do what you need to insert your class into the stream

stream << r.ToString();

// now return the stream

return stream;
}



This code assumes that YourClass defines a method named ToString() that transforms an instance of YourClass into a meaningful string.

I hope this helps.

[edited by - Digitalfiend on May 3, 2003 3:28:31 PM]

Share this post


Link to post
Share on other sites
Hi again.

Is this what you ment returning reference or shometing...

  
// this is from ostream.h

inline ostream& ostream::operator<<(const unsigned char * _s)
{
return operator<<((const char *) _s);
}

To me that looks like a endles loop and so it was when I tryed to implement it?

Share this post


Link to post
Share on other sites
You need to return a reference to what the this pointer points to, i.e. the object that invoked operator<<():


class CTest
{
public:
    CTest& operator<<(const std::string& str);

private:
    ...
};

inline CTest& CTest::operator<<(const std::string& str)
{
    // do stuff

    return *this;
}


[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on May 4, 2003 10:22:55 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Lektrix
You need to return a reference to what the this pointer points to, i.e. the object that invoked operator<<():


class CTest
{
public:
    CTest& operator<<(const std::string& str);

private:
    ...
};

inline CTest& CTest::operator<<(const std::string& str)
{
    // do stuff

    return *this;
}


[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on May 4, 2003 10:22:55 AM]


Thanks Lektrix got it working. And thanks to all of you.

Share this post


Link to post
Share on other sites