C Board  

Go Back   C Board > General Programming Boards > FAQ Board

 
 
LinkBack Thread Tools Display Modes
Old 01-16-2003, 04:44 PM   #1
Registered User
 
Join Date: Dec 2002
Posts: 6
substituting cout

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
JerryL_MB is offline  
Old 01-16-2003, 05:07 PM   #2
Registered User
 
Join Date: Sep 2002
Posts: 1,640
I think macro's are what you're looking for,
Don't know exactly how to use it with 'cout' though
Travis Dane is offline  
Old 01-16-2003, 05:14 PM   #3
Shadow12345
Guest
 
Posts: n/a
#define cout my_own_cheese
 
Old 01-16-2003, 06:02 PM   #4
Programming Sex-God
 
Polymorphic OOP's Avatar
 
Join Date: Nov 2002
Posts: 1,078
Quote:
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!
Polymorphic OOP is offline  
Old 01-16-2003, 06:03 PM   #5
and the hat of marbles
 
Sang-drax's Avatar
 
Join Date: May 2002
Location: Göteborg, Sweden
Posts: 2,038
No, #defines are evil

Code:
std::ostream& my_own__cout = cout;
__________________
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
Sang-drax is offline  
Old 01-18-2003, 12:20 PM   #6
Registered User
 
Join Date: Dec 2002
Posts: 6
Thanx sang-drax, the code is working. But can someone explain it to me a little bit?

Code:
std::ostream& my_own_cout = cout;
why it is not enough to write:

Code:
ostream my_own_cout;

or

std::ostream my_own_cout;
if cout is an object of class ostream?

Thank you in advance!
JerryL_MB is offline  
Old 01-18-2003, 12:55 PM   #7
Registered User
 
Join Date: Sep 2002
Posts: 272
>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.
__________________
Joe
JoeSixpack is offline  
Old 01-18-2003, 01:02 PM   #8
Registered User
 
rmullen3's Avatar
 
Join Date: Nov 2001
Posts: 330
~

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 =)
rmullen3 is offline  
 

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wiki FAQ dwks General Discussions 192 04-29-2008 01:17 PM
Redirect FAQ... Queatrix General Discussions 21 05-21-2007 04:48 PM
printf vs cout RyeDunn C++ Programming 5 07-09-2002 04:26 PM
FAQ cout evilmonkey FAQ Board 1 10-07-2001 11:32 AM
What's wrong with Micorsoft Visual C++? knave C++ Programming 9 10-04-2001 08:02 AM


All times are GMT -6. The time now is 10:21 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22