By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,327 Members | 1,781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,327 IT Pros & Developers. It's quick & easy.

a stream manipulator

P: n/a
ax
my book says it is ok define

ostream& operator<<(ostream& (*p)(ostream ))
{ return (*p)( *this); }

ostream& endl(ostream& o)
{o.put('\n'); o.flush();}

but seems compiler says it is not correct because "<<" needs 2
arguments, so i wrote

ostream& operator<<(ostream& ostr, ostream& (*p)(ostream& ))
{ return (*p)( ostr ); }

ostream& endl(ostream& o)
{o.put('\n'); o.flush();}
and the compiler compile and run it when i write
cout << "jfjfjfj" << endl;

But i have some doubit. is the last right?

then i have not good understanding for objects ostream "ios" flags
"internal, uppercase". What is their purpose?

for istream "ios" what is the pourpose of
"left, right, internal, showbase, showpoint, uppercase, showpos,
scientific, fixed, "?
Jun 29 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ax wrote:
my book says it is ok define

ostream& operator<<(ostream& (*p)(ostream ))
{ return (*p)( *this); }
I hope the book also says that it's OK to define it *only* as
a *member* of 'ostream'. Otherwise, what's "this" doing there?
ostream& endl(ostream& o)
{o.put('\n'); o.flush();}

but seems compiler says it is not correct because "<<" needs 2
arguments, so i wrote

ostream& operator<<(ostream& ostr, ostream& (*p)(ostream& ))
{ return (*p)( ostr ); }
OK.
ostream& endl(ostream& o)
{o.put('\n'); o.flush();}
and the compiler compile and run it when i write
cout << "jfjfjfj" << endl;

But i have some doubit. is the last right?
Seems fine. If it compiles and does what you need, what could be
the problem? B-)
then i have not good understanding for objects ostream "ios" flags
"internal, uppercase". What is their purpose?
What does your favourite C++ book have to say about them? Are
you reading Kuehl and Langer's book on streams?
for istream "ios" what is the pourpose of
"left, right, internal, showbase, showpoint, uppercase, showpos,
scientific, fixed, "?


Get a good book that describes streams. Angelika Langer and Klaus
Kreft published one several years back, basically a textbook on C++
streams (ISBN: 0201183951).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 29 '06 #2

P: n/a
* ax:
my book says it is ok define

ostream& operator<<(ostream& (*p)(ostream ))
{ return (*p)( *this); }
Which book is that?

This could be a member function.

ostream& endl(ostream& o)
{o.put('\n'); o.flush();}

but seems compiler says it is not correct because "<<" needs 2
arguments, so i wrote

ostream& operator<<(ostream& ostr, ostream& (*p)(ostream& ))
{ return (*p)( ostr ); }

ostream& endl(ostream& o)
{o.put('\n'); o.flush();}
and the compiler compile and run it when i write
cout << "jfjfjfj" << endl;

But i have some doubit. is the last right?
No. You need to return a reference to the 'o' argument. Also, it's not
a good idea to use the same names as things in the std namespace.

then i have not good understanding for objects ostream "ios" flags
"internal, uppercase". What is their purpose?
I don't think the first is standard, although it could be. The second
sounds like a standard one. Look them up in your /documentation/.

for istream "ios" what is the pourpose of
"left, right, internal, showbase, showpoint, uppercase, showpos,
scientific, fixed, "?


Whether this is HOMEWORK or not, look them up in your /documentation/.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 29 '06 #3

P: n/a
ax
On Thu, 29 Jun 2006 16:31:03 +0200, "Alf P. Steinbach"
<al***@start.no> wrote:
* ax:
my book says it is ok define

ostream& operator<<(ostream& (*p)(ostream ))
{ return (*p)( *this); }
Which book is that?

This could be a member function.


if i have the
class X{
public:
X(){}
~X(){}
friend int f(X, int);
friend ostream& operator<<(ostream& , X& );
X& operator=(X&);
X& g(int, X&);
};

"member functions" for class X are all functions for the class X?
(X() ~X(), f, <<, =, g)
or they are there only "=", "g"?
ostream& endl(ostream& o)
{o.put('\n'); o.flush();}

but seems compiler says it is not correct because "<<" needs 2
arguments, so i wrote

ostream& operator<<(ostream& ostr, ostream& (*p)(ostream& ))
{ return (*p)( ostr ); }

ostream& endl(ostream& o)
{o.put('\n'); o.flush();}
and the compiler compile and run it when i write
cout << "jfjfjfj" << endl;

But i have some doubit. is the last right? No. You need to return a reference to the 'o' argument. Also, it's not
a good idea to use the same names as things in the std namespace.


return o;
ok i forgot to write it in the last endl (i copied the one of the book
and not the one in my code)
then i have not good understanding for objects ostream "ios" flags
"internal, uppercase". What is their purpose?


I don't think the first is standard, although it could be. The second
sounds like a standard one. Look them up in your /documentation/.

for istream "ios" what is the pourpose of
"left, right, internal, showbase, showpoint, uppercase, showpos,
scientific, fixed, "?


Whether this is HOMEWORK or not, look them up in your /documentation/.


what meaning is to define a "right" indentation for an input stream?
the same for showbase

is it the meaning: prog tell me it wants a hex value and i write 9 and
it doesn't accept it because the input stream has in its flag
"showbase" on?

Jun 29 '06 #4

P: n/a
ax
On Thu, 29 Jun 2006 10:27:56 -0400, "Victor Bazarov"
<v.********@comAcast.net> wrote:
ax wrote:
my book says it is ok define But i have some doubit. is the last right?
Seems fine. If it compiles and does what you need, what could be
the problem? B-)


my good C++ compiler allows all i imagine and all seems goes right
but i would like some feedback from humans because i see that everyone
use template "class <etc>" but i don't understand well, and don't
understand why ( because it seems i have all that i need without them)
then i have not good understanding for objects ostream "ios" flags
"internal, uppercase". What is their purpose?


What does your favourite C++ book have to say about them? Are
you reading Kuehl and Langer's book on streams?


no. i like the book i read
for istream "ios" what is the pourpose of
"left, right, internal, showbase, showpoint, uppercase, showpos,
scientific, fixed, "?


Get a good book that describes streams. Angelika Langer and Klaus
Kreft published one several years back, basically a textbook on C++
streams (ISBN: 0201183951).


basically i reinvent the hot water and so i write the ostream,
istream, strstram classes (but they are different) so i would question
why the use of these flags in an input stream
V

thank you
Jun 29 '06 #5

P: n/a
ax
On Thu, 29 Jun 2006 19:53:36 +0200, "ax" <aa@aa.aaa> wrote:
for istream "ios" what is the pourpose of
"left, right, internal, showbase, showpoint, uppercase, showpos,
scientific, fixed, "?


Whether this is HOMEWORK or not, look them up in your /documentation/.


what meaning is to define a "right" indentation for an input stream?
the same for showbase

is it the meaning: prog tell me it wants a hex value and i write 9 and
it doesn't accept it because the input stream has in its flag
"showbase" on?


so is there no answer?
Jun 30 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.