How loop std::ostream and std::istream?

I was reading the book C++: How to program (Deitel, 6th Edition), and on the page 602 I implement my own String (training). But I want to implement my own StringStream. So, how can I make a loop like stringstream, and accept all kind of data?
E.G
1
2
3
double PI = 3.14;
std::stringstream a;
a << "The value of PI with precision 2 is " << PI; 

Should I use templates? How do std::ostream loops work?
Thanks!

Last edited on
Last edited on
Anyway, how can I make one stringstream (just the operators << and >>)?
When you say:

Anyway, how can I make one stringstream (just the operators << and >>)?


Do you mean, in your own class?

When you say:

Anyway, how can I make one stringstream (just the operators << and >>)?


Do you mean, in your own class?


Yes.
How about this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class MyClass
{
public:
	
   ...

   friend std::ostream& operator << (std::ostream& os, const MyClass& obj)
   {
      os << obj.var;
      return os;
   }

   ...

private:
   int var;
};
Last edited on
@ajh32 Stringstreams just get the value from any numerical \ char variable and put it on a private char*. That's the challenge;
I'm pretty sure that stringstreams use a std::string internally in modern implementations.
Last edited on
Topic archived. No new replies allowed.