Operator Overinding

M

Mongoose7

Hi everyone,

Ive been trying to overload the insertion and extration operators (<<
and >>) without much success... What I need to do is

override a general cout << anyvar << anyvar

with a overriddden method that adds a timestamp to the cout.

[Timestamp] anyvar anyvar

OR

is it possible to mess around with streams so that I can actually
stream something through a class of my own, like

myclass << somevar

Thanks!
 
M

Maxim Yegorushkin

Hi everyone,

Ive been trying to overload the insertion and extration operators (<<
and >>) without much success... What I need to do is

override a general cout << anyvar << anyvar

with a overriddden method that adds a timestamp to the cout.

[Timestamp] anyvar anyvar

One simple approach is to create a manipulator class that will put the
stamp and the syntax would look like:

std::cout << stamp() << anyvar << anyvar;

Another more advanced approach is to use a custom streambuf which will
track ends of lines and insert stamping for you. Google groups for
filtering streambuf.
OR

is it possible to mess around with streams so that I can actually
stream something through a class of my own, like

myclass << somevar

Yes, it's pretty easy to wrap and std::eek:stream into your own class with a
templated operatro<<(T const&) which does needed profixing and postfixing
and forward t to operator<<(std::eek:stream&, T).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,298
Messages
2,571,542
Members
48,283
Latest member
RitaVui655

Latest Threads

Top