Converting a struct to a stream

J

Jacek Dziedzic

The said:
I have created the following conversion in the header file
for my struct called SomeStruct.

operator std::string () {return "some useless text";}

I'm using it as follows.

SomeStruct someS;
std:cout << (std::string)someS;

This far, everything works just fine. Now, i got lazy of
constantly writing the string-part so i thought i'd like to
extend my struct to be convertible to a stream. So i
went as follows.

operator std::eek:stream () {return "some other text";}

This didn't work, however. I don't understand the error
messages either. Any hints?

Also, would it be a smarter idea to overload << operator
for my struct?

Definitely.
How do i do that? I've played around
with friend-keyword but frankly, i must be missing
something, getting nowhere. Any help is appreciated...

You need a standalone function, not a member of a
class. Something like

ostream& operator<<(ostream& whither, const yourstruct& what) {
whither << "some useless text";
return whither;
}

HTH,
- J.
 
T

The Cool Giraffe

I have created the following conversion in the header file
for my struct called SomeStruct.

operator std::string () {return "some useless text";}

I'm using it as follows.

SomeStruct someS;
std:cout << (std::string)someS;

This far, everything works just fine. Now, i got lazy of
constantly writing the string-part so i thought i'd like to
extend my struct to be convertible to a stream. So i
went as follows.

operator std::eek:stream () {return "some other text";}

This didn't work, however. I don't understand the error
messages either. Any hints?

Also, would it be a smarter idea to overload << operator
for my struct? How do i do that? I've played around
with friend-keyword but frankly, i must be missing
something, getting nowhere. Any help is appreciated...
 
F

Fei Liu

The said:
I have created the following conversion in the header file
for my struct called SomeStruct.

operator std::string () {return "some useless text";}

I'm using it as follows.

SomeStruct someS;
std:cout << (std::string)someS;

This far, everything works just fine. Now, i got lazy of
constantly writing the string-part so i thought i'd like to
extend my struct to be convertible to a stream. So i
went as follows.

operator std::eek:stream () {return "some other text";}

This didn't work, however. I don't understand the error
messages either. Any hints?

Also, would it be a smarter idea to overload << operator
for my struct? How do i do that? I've played around
with friend-keyword but frankly, i must be missing
something, getting nowhere. Any help is appreciated...
Try operator std::eek:stream & (std::eek:stream & os) {return os << "some
other text";}

First of all you can't construct std::eek:stream from char *, and secondly
std::eek:stream is not copy constructible. Your best bet is to override
'<<' such as:
operator std::eek:stream & << (std::eek:stream & os) { return os << "some text"; }

Fei
 
R

Ralph D. Ungermann

Jacek said:
You need a standalone function, not a member of a
class. Something like

ostream& operator<<(ostream& whither, const yourstruct& what) {


And probably (if you need access to private members):

class yourstruct
{
// your stuff
friend ostream& operator<<(ostream&, const yourstruct&);
}
 
T

The Cool Giraffe

Ralph D. Ungermann wrote/skrev/kaita/popisal/schreibt :
Jacek Dziedzic wrote:

And probably (if you need access to private members):

class yourstruct {
// your stuff
friend ostream& operator<<(ostream&, const yourstruct&); }


Thanks. Now, some wonderings i have about your answer.
1. What happened to the names of the parameters? I was
expecting something like the below.
class yourstruct {
// your stuff
friend ostream& operator<<(ostream& param1, const yourstruct& param2); }

2. How to implement the method itself? I'm guessing
somewhere in the lines of this:

param1 = "something";
return param1;

or perhaps

param1 << "something";
return param1;

but is the above a legal C++ code? I know some things
do work but are not really that good to use. Hence my
wondering. How would YOU implement that function?
 
J

Jacek Dziedzic

The said:
Ralph D. Ungermann wrote/skrev/kaita/popisal/schreibt :


Thanks. Now, some wonderings i have about your answer.
1. What happened to the names of the parameters? I was
expecting something like the below.

In declarations you can omit them.
2. How to implement the method itself? I'm guessing
somewhere in the lines of this:

param1 = "something";
return param1;

I don't think you can assign a string to a stream.
or perhaps

param1 << "something";
return param1;

This one.
but is the above a legal C++ code?

This one is. Remember to return the stream, this
will allow for chaining multiple <<'s together.
> I know some things
do work but are not really that good to use. Hence my
wondering. How would YOU implement that function?

Just like you did in:
> param1 << "something";
> return param1;

BTW, you are not "converting" a struct to a stream,
rather sending it there or perhaps serializing would be
a better word.

HTH,
- J.
 
R

Ralph D. Ungermann

The said:
Ralph D. Ungermann wrote/skrev/kaita/popisal/schreibt :
Jacek Dziedzic wrote:
You need [...] Something like

ostream& operator<<(ostream& whither, const yourstruct& what) {

And probably (if you need access to private members):

class yourstruct {
// your stuff
friend ostream& operator<<(ostream&, const yourstruct&); }

Thanks. Now, some wonderings i have about your answer.
1. What happened to the names of the parameters?

Feel free to provide argument names as you like. They have no effect,
unless you _define_ the function; in the body, these are the names you
have to use in your implementation (*).

I've omitted the names, because the types are self-explaining here.
But I'd definitely provide names there:
bool resize( int, int, int, int, bool );
2. How to implement the method itself?

ostream& operator<<(ostream& os, const yourstruct& what)
{
os << "{";
if ( what.ok() )
os << what.private_x << ", " << what.private_y;
else
os << "(undefined)";
os << "}"; // definitely NO std::endl here!

return os;
}

N.B.: Stream modifiers probably won't work as expected:
std::cout << std::setw(8) << yourstruct(17, 4) << std::endl;
// will look ugly
 
T

The Cool Giraffe

Jacek Dziedzic wrote/skrev/kaita/popisal/schreibt :
In declarations you can omit them.


I don't think you can assign a string to a stream.


This one.


This one is. Remember to return the stream, this
will allow for chaining multiple <<'s together.


Just like you did in:


BTW, you are not "converting" a struct to a stream,
rather sending it there or perhaps serializing would be
a better word.


Right, got it. Thank you very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top