cout used for more than just text

This is a discussion on cout used for more than just text within the C++ Programming forums, part of the General Programming Boards category; Would it be a good idea to use cout with a new class to display something other than a line ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    127

    cout used for more than just text

    Would it be a good idea to use cout with a new class to display something other than a line of text? Like say for example you have a 3d object class. Could you use the friend ostream/3d_object function to say render the object instead of printing a line?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, what are the odds that cout is something that can "render the object"? (Remember what the c stands for in cout.)

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    127
    If it's your class and you make the definition for the cout function whats stopping you from using it to render the object? I get it c stands for character but what if you made a new ostream variable with the name changed to support some sort of 3d rendering? e.g

    Code:
    ostream render;
    render<<triangle;
    Ok now I look dumb because I don't know how to add code. lol.
    Nvm. I got it.
    Last edited by herWter; 09-07-2008 at 06:30 PM.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    I get it c stands for character
    No, I think it stands for Console.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No, it's character, at least according to Stroustrup's FAQs.

    Anyway, nothing's wrong with the code you have, it just has nothing to do with cout.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    127
    I wasn't using cout in the code.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Quote Originally Posted by tabstop View Post
    No, it's character, at least according to Stroustrup's FAQs.

    Anyway, nothing's wrong with the code you have, it just has nothing to do with cout.
    Whoops, I stand duly corrected!

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by herWter View Post
    If it's your class and you make the definition for the cout function whats stopping you from using it to render the object? I get it c stands for character but what if you made a new ostream variable with the name changed to support some sort of 3d rendering? e.g

    Code:
    ostream render;
    render<<triangle;
    In a way, I suppose you could. iostreams is supposed to be abstractions that "control input from and output to character sequences". So, you still have to honor its "contract" in some way. How would this new class (I'm assuming derived from ostream) handle this?

    Code:
    render << "Hello, World!" << endl;

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Presumably, to render a 3D object, you mean to draw the 3D object on a display. That is not a character operation, and C++ stream objects are not suitable for that [unless you have a very very old-fashioned graphical terminal [20 or more years old that is], where drawing is actually done by sending "text" sequences to the terminal - and you would still have to come up with what it should look like in some way as pixels, and THEN translate that to "text"].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Basically there should be nothing stopping you from creating a class that has overloaded the << operator, so the usage looks similar to streams, but I'm afraid there is no point in attempting to inherit it from streams etc. Streams are all about how different types are represented as character strings (should numbers be displayed in decimal or hexadecimal, scientific or nonscientific notation etc).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    I think it's a pretty good idea, with flaws.

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    127
    >>I'm assuming derived from ostream

    No. Just a friend function for the ostream with the new class.

    >>render << "Hello, World!" << endl;

    I changed my mind. I would use cout. For the endl thing, it would advance to the next line in the console window as it regularly would.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by herWter View Post
    >>I'm assuming derived from ostream

    No. Just a friend function for the ostream with the new class.

    >>render << "Hello, World!" << endl;

    I changed my mind. I would use cout. For the endl thing, it would advance to the next line in the console window as it regularly would.
    That's just crazy. Do you also think that a dishwasher [the machine many people have in the kitchen or thereabouts, not a person] should be suitably fitted with an umbrella?

    ostream is for sending text to an output. rendering 3D objects is a graphical operation that has NOTHING to do with ostream functionality. Just like cleaning plates has nothign to do with umbrellas.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Jun 2008
    Posts
    127
    It might just be me but when I think of cout I just think of general output.
    I just thought it would be more user friendly if you replaced something like this...

    Code:
    3d_object triangle(10,40,50);
    triangle.draw();
    With some thing like this...

    Code:
    3d_object triangle(10,40,50);
    cout<<triangle;
    Other than the name I don't think cout suggests in any way limits to just text output.(I mean just the operator<< function, not all the other supporter functions)

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If I correctly understand what you are wanting to do, I would highly recommend that you just make a class that is derived from ostream that does all the bells and whistles that you are hoping to demonstrate. There is absolutely no reason to be altering cout in any way that you have described. The issue at hand is utility. Useful is not always the same as "looks cool." Its not even necessarily similar to "looks interesting."

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  2. Need Help with a Bowling Score Program
    By oobootsy1 in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2005, 09:04 AM
  3. Homework due tonight! help please!!
    By Andy717 in forum C++ Programming
    Replies: 41
    Last Post: 04-07-2005, 01:18 PM
  4. how to cout colored text?
    By Guanhui in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2002, 08:14 AM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM

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