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?
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?
ostream is strictly a base class. What you probably want is something like an array of ostream*.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
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); //}
Thanks for the replies.
I suppose if there was a copy constructor then one could initialize like this:
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.Code:std::ostream array_of_streams[3] = { std::ostream( std::cout.rdbuf() ), std::ostream( std::cout.rdbuf() ), std::ostream( std::cout.rdbuf() ) };
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!?
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