Thread: FAQ: substituting cout (C++)

  1. #1
    Registered User
    Join Date
    Dec 2002

    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?


  2. #2
    Registered User
    Join Date
    Sep 2002
    I think macro's are what you're looking for,
    Don't know exactly how to use it with 'cout' though

  3. #3
    #define cout my_own_cheese

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    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!

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Göteborg, Sweden
    No, #defines are evil

    std::ostream& my_own__cout = cout;
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User
    Join Date
    Dec 2002
    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;
    std::ostream my_own_cout;
    if cout is an object of class ostream?

    Thank you in advance!

  7. #7
    Registered User
    Join Date
    Sep 2002
    >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.

  8. #8
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001


    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 =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of
    Replies: 192
    Last Post: 04-29-2008, 01:17 PM
  2. Redirect FAQ...
    By Queatrix in forum A Brief History of
    Replies: 21
    Last Post: 05-21-2007, 04:48 PM
  3. printf vs cout
    By RyeDunn in forum C++ Programming
    Replies: 5
    Last Post: 07-09-2002, 04:26 PM
  4. FAQ cout
    By evilmonkey in forum FAQ Board
    Replies: 1
    Last Post: 10-07-2001, 11:32 AM
  5. What's wrong with Micorsoft Visual C++?
    By knave in forum C++ Programming
    Replies: 9
    Last Post: 10-04-2001, 08:02 AM