Cout style output
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" ?
Your insertion operator (the formal name for our friend Mr. << ) needs to return a reference to the class so you can "chain" them together.
And you shouldn't use
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]
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]
The insertion operator should be defined as a binary operator outside the scope of the class, like so:
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]
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]
endl is not just a newline character. In addition to inserting a ''/n,'' it flushes the buffer.
Hi again.
Is this what you ment returning reference or shometing...
To me that looks like a endles loop and so it was when I tryed to implement it?
Is this what you ment returning reference or shometing...
// this is from ostream.hinline 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?
You need to return a reference to what the
[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on May 4, 2003 10:22:55 AM]
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]
quote:Original post by Lektrix
You need to return a reference to what thethis
pointer points to, i.e. the object that invokedoperator<<()
:
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement