Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > iostream::write for a composite class

Reply
Thread Tools

iostream::write for a composite class

 
 
kk_oop@yahoo.com
Guest
Posts: n/a
 
      07-19-2008
Consider class X and Y where:

class X
{
double d;
int i;
Y myY;
};

class Y
{
int h;
int g;
};

If I pass the address of an instance of X (cast to a char *) to a
binary ofstream's write() method, indicating sizeof(X) as the size of
the data, would that write out all of X, including the state of its Y
instance?

Thanks!

Ken
 
Reply With Quote
 
 
 
 
sonison.james@gmail.com
Guest
Posts: n/a
 
      07-19-2008
On Jul 19, 7:50*pm, (E-Mail Removed) wrote:
> Consider class X and Y where:
>
> class X
> {
> * double d;
> * int i;
> * Y myY;
>
> };
>
> class Y
> {
> * int h;
> * int g;
>
> };
>
> If I pass the address of an instance of X (cast to a char *) to a
> binary ofstream's write() method, indicating sizeof(X) as the size of
> the data, would that write out all of X, including the state of its Y
> instance?
>
> Thanks!
>
> Ken


Sure, here's an example:

#include <iostream>
#include <fstream>
using namespace std;

struct X
{
int x;
int y;
};

struct Y
{
int a;
int b;
X x;
};

int main()
{
Y y;

y.a = 1;
y.b = 2;
y.x.x = 3;
y.x.y = 4;

ofstream out( "tmp.bin", ios_base::binary );
out.write( reinterpret_cast< char* >( &y ), sizeof( Y ) );
out.close();

ifstream in( "tmp.bin", ios_base::binary );
Y y2;
in.read( reinterpret_cast< char* >( &y2 ), sizeof( Y ) );

cout << "y2.a=" << y2.a << endl;
cout << "y2.b=" << y2.b << endl;
cout << "y2.x.x=" << y2.x.x << endl;
cout << "y2.x.y=" << y2.x.y << endl;
}

But if you are interested in object boost has some very useful
classes: http://www.boost.org/doc/libs/1_35_0...doc/index.html
 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      07-19-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Consider class X and Y where:
>
> class X
> {
> double d;
> int i;
> Y myY;
> };
>
> class Y
> {
> int h;
> int g;
> };


Practically you have to define Y before you declare the member 'myY' as
type of Y.

>
> If I pass the address of an instance of X (cast to a char *) to a
> binary ofstream's write() method, indicating sizeof(X) as the size of
> the data, would that write out all of X, including the state of its Y
> instance?


Yes

--
Best Regards
Barry
 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      07-19-2008
On Jul 19, 11:25 am, (E-Mail Removed) wrote:
> On Jul 19, 7:50 pm, (E-Mail Removed) wrote:
>
>
>
> > Consider class X and Y where:

>
> > class X
> > {
> > double d;
> > int i;
> > Y myY;

>
> > };

>
> > class Y
> > {
> > int h;
> > int g;

>
> > };

>
> > If I pass the address of an instance of X (cast to a char *) to a
> > binary ofstream's write() method, indicating sizeof(X) as the size of
> > the data, would that write out all of X, including the state of its Y
> > instance?

>
> > Thanks!

>
> > Ken

>
> Sure, here's an example:
>
> #include <iostream>
> #include <fstream>
> using namespace std;
>
> struct X
> {
> int x;
> int y;
>
> };
>
> struct Y
> {
> int a;
> int b;
> X x;
>
> };
>
> int main()
> {
> Y y;
>
> y.a = 1;
> y.b = 2;
> y.x.x = 3;
> y.x.y = 4;
>
> ofstream out( "tmp.bin", ios_base::binary );
> out.write( reinterpret_cast< char* >( &y ), sizeof( Y ) );
> out.close();
>
> ifstream in( "tmp.bin", ios_base::binary );
> Y y2;
> in.read( reinterpret_cast< char* >( &y2 ), sizeof( Y ) );
>
> cout << "y2.a=" << y2.a << endl;
> cout << "y2.b=" << y2.b << endl;
> cout << "y2.x.x=" << y2.x.x << endl;
> cout << "y2.x.y=" << y2.x.y << endl;
>
> }

Argh, avoid this, you hear me - AVOID this. reinterpret_cast is
equal to trouble in 99% of cases (and no one is aware of 1%, we just
have it to be politically correct). This is likely not to work if you
serialize this way on one machine, and deserialize on another;
certainly won't work on machines with different indianess, 32 vs. 64
bits, you name it.

Just write your own ostream<< operator to write to a file, lest
succumb to poorly implemented features which wreak more havoc than
provide a panacea.


Add this to your classes, if you can:

friend ostream & operator << (ostream &os, const X & o);
friend ostream & operator << (ostream &os, const Y & o);

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-20-2008
On Jul 19, 1:50 pm, (E-Mail Removed) wrote:
> Consider class X and Y where:


> class X
> {
> double d;
> int i;
> Y myY;
> };


> class Y
> {
> int h;
> int g;
> };


> If I pass the address of an instance of X (cast to a char *)
> to a binary ofstream's write() method, indicating sizeof(X) as
> the size of the data, would that write out all of X, including
> the state of its Y instance?


Maybe. That's going to require a reinterpret_cast, so whatever
happens is really implementation defined. But you'll probably
find all of the bits you want in there somewhere. Plus perhaps
a few others, and of course, in an unspecified order.

Basically, using ostream::write is fine, but unless the data
you're writing is preformatted into a buffer of char, you can't
count on being able to reread it in any way.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-20-2008
On Jul 20, 1:43 am, puzzlecracker <(E-Mail Removed)> wrote:
> On Jul 19, 11:25 am, (E-Mail Removed) wrote:


[...]
> Argh, avoid this, you hear me - AVOID this. reinterpret_cast is
> equal to trouble in 99% of cases (and no one is aware of 1%, we just
> have it to be politically correct).


I am. Ever try to write a garbage collector.

(But your comments are right on target here.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
kk_oop@yahoo.com
Guest
Posts: n/a
 
      07-20-2008
On Jul 19, 11:25*am, (E-Mail Removed) wrote:
> On Jul 19, 7:50*pm, (E-Mail Removed) wrote:
>

<<snip>>
>


> Sure, here's an example:
>
> #include <iostream>
> #include <fstream>
> using namespace std;
>
> struct X
> {
> * * * * int x;
> * * * * int y;
>
> };
>
> struct Y
> {
> * * * * int a;
> * * * * int b;
> * * * * X x;
>
> };
>
> int main()
> {
> * * * * Y y;
>
> * * * * y.a = 1;
> * * * * y.b = 2;
> * * * * y.x.x = 3;
> * * * * y.x.y = 4;
>
> * * * * ofstream out( "tmp.bin", ios_base::binary );
> * * * * out.write( reinterpret_cast< char* >( &y ), sizeof( Y ) );
> * * * * out.close();
>
> * * * * ifstream in( "tmp.bin", ios_base::binary );
> * * * * Y y2;
> * * * * in.read( reinterpret_cast< char* >( &y2 ), sizeof( Y ) );
>
> * * * * cout << "y2.a=" << y2.a << endl;
> * * * * cout << "y2.b=" << y2.b << endl;
> * * * * cout << "y2.x.x=" << y2.x.x << endl;
> * * * * cout << "y2.x.y=" << y2.x.y << endl;
>
> }
>
> But if you are interested in object boost has some very useful
> classes:http://www.boost.org/doc/libs/1_35_0...oc/index..html


Would this break if X or Y has a virtual function? Would sizeof then
include the pointer to the virtual table--and would write() try to
write that information?

Thanks again,

Ken
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-20-2008
On Jul 20, 12:06 pm, Peter Jansson <(E-Mail Removed)> wrote:
> On Sat, 19 Jul 2008, puzzlecracker wrote:


> /.../> Argh, avoid this, you hear me - AVOID this. reinterpret_cast is
> > equal to trouble in 99% of cases (and no one is aware of 1%, we just
> > have it to be politically correct). This is likely not to work if you
> > serialize this way on one machine, and deserialize on another;
> > certainly won't work on machines with different indianess, 32 vs. 64
> > bits, you name it.


> /.../


> Unformatted output and input could be Ok if staying on one
> machine or one type of machine only.


And if you never upgrade or change your compiler, and if you
always compile with exactly the same options.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-20-2008
On Jul 20, 4:06 pm, (E-Mail Removed) wrote:
> On Jul 19, 11:25 am, (E-Mail Removed) wrote:
> > On Jul 19, 7:50 pm, (E-Mail Removed) wrote:


> > Sure, here's an example:


> > #include <iostream>
> > #include <fstream>
> > using namespace std;


> > struct X
> > {
> > int x;
> > int y;
> > };


> > struct Y
> > {
> > int a;
> > int b;
> > X x;
> > };


> > int main()
> > {
> > Y y;


> > y.a = 1;
> > y.b = 2;
> > y.x.x = 3;
> > y.x.y = 4;


> > ofstream out( "tmp.bin", ios_base::binary );
> > out.write( reinterpret_cast< char* >( &y ), sizeof( Y ) );
> > out.close();


> > ifstream in( "tmp.bin", ios_base::binary );
> > Y y2;
> > in.read( reinterpret_cast< char* >( &y2 ), sizeof( Y ) );


> > cout << "y2.a=" << y2.a << endl;
> > cout << "y2.b=" << y2.b << endl;
> > cout << "y2.x.x=" << y2.x.x << endl;
> > cout << "y2.x.y=" << y2.x.y << endl;
> > }


> Would this break if X or Y has a virtual function?


It's broken as it stands, even without virtual functions.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Composite vs non composite Controls Mike ASP .Net Web Controls 4 03-11-2005 05:47 AM
Losing Composite Control property that another Composite Control ... Chad ASP .Net Building Controls 0 02-01-2005 09:01 PM
Possible to create a composite control that has a child control that is a validator that validates the composite control itself? Jonathan Eric Miller ASP .Net Building Controls 2 07-22-2004 10:58 PM
Composite control with dynamic composite controls sleigh ASP .Net 1 02-12-2004 06:24 PM



Advertisments