Quick question: Array/Vector of std::ostream ?

This is a discussion on Quick question: Array/Vector of std::ostream ? within the C++ Programming forums, part of the General Programming Boards category; How do you create an array of std:: ostream objects? It has no default constructor so it doesn't seem to ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Question Quick question: Array/Vector of std::ostream ?

    How do you create an array of std:: ostream objects?

    It has no default constructor so it doesn't seem to be possible... is there a way?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,639
    ostream is strictly a base class. What you probably want is something like an array of ostream*.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by kikazaru View Post
    How do you create an array of std:: ostream objects?

    It has no default constructor so it doesn't seem to be possible... is there a way?
    It isn't the lack of a default constructor, but the lack of a copy constructor, which makes this impossible. You can't have a vector of references, so you must use a vector of pointers... and that means you have to properly manage object lifetimes. It's going to suck.

    In general, any sort of RAII object is very difficult to put into a vector.

    What are you trying to do?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    Thanks for the replies.

    I suppose if there was a copy constructor then one could initialize like this:

    Code:
    std::ostream array_of_streams[3] = { std::ostream( std::cout.rdbuf() ), 
                                                                 std::ostream( std::cout.rdbuf() ),
                                                                 std::ostream( std::cout.rdbuf() ) };
    Wouldn't this still want to default construct the elements before the copy constructor is called? It doesn't seem to need to, so I guess not; but at the same time, I don't see why it can't just create them all in place.

    It's not very important, I wanted to make an array of output streams so I could easily switch the level of reporting by setting the rdbuf to 0, or std::cout.rdbuf().

    I realize I could do it with pointers...

    But, ... but... why can't I initialize array elements in place!?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    One of the great questions of C++ ...

    You'll be able to do it in C++0x, I think.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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