FAQ: substituting cout (C++) [Archive] - C Board

PDA

View Full Version : FAQ: substituting cout (C++)


JerryL_MB
01-16-2003, 04:44 PM
As I know, cout is object of an ostream class. Am I right?

I have the following question: I would like to use another 'command' to output data on the screen.
e.g. instead of

cout << "abc";

i would like to use

my_own_cout << "abc";


I have tried to declare it:
ostream my_own_cout;
But it didn't work. Why is that so?

How to make a 'substitute' for cout?

TNX

Travis Dane
01-16-2003, 05:07 PM
I think macro's are what you're looking for,
Don't know exactly how to use it with 'cout' though

Shadow12345
01-16-2003, 05:14 PM
#define cout my_own_cheese

Polymorphic OOP
01-16-2003, 06:02 PM
Originally posted by Shadow12345
#define cout my_own_cheese

You mixed up the order, it would be

#define my_own_cheese cout

Still, Sang-drax is right, #defines are evil!

Sang-drax
01-16-2003, 06:03 PM
No, #defines are evil (http://www.research.att.com/~bs/bs_faq2.html#macro)


std::ostream& my_own__cout = cout;

JerryL_MB
01-18-2003, 12:20 PM
Thanx sang-drax, the code is working. But can someone explain it to me a little bit?


std::ostream& my_own_cout = cout;


why it is not enough to write:


ostream my_own_cout;

or

std::ostream my_own_cout;


if cout is an object of class ostream?

Thank you in advance!

JoeSixpack
01-18-2003, 12:55 PM
>std::ostream& my_own_cout = cout;

Is creating a reference (alias) to the original cout. my_own_cout isn't a seperate object. If you really want a seperate ostream object you could do -

ostream my_own_cout(cout.rdbuf());

Which will point your ostream streambuf pointer at the same one cout uses. Or if that's not enough you could derive you're own streambuf class an implement all the system calls, etc and assign it to your ostream object.

rmullen3
01-18-2003, 01:02 PM
cout is the standard output device, so you can access it directly, under a different name by using a reference. To declare an ostream object you have to pass in a streambuf pointer, I don't think it has a default constructor.

EDIT: as was already said... hehe =)