473,395 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

iostream::write for a composite class

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
Jul 19 '08 #1
9 1796
On Jul 19, 7:50*pm, kk_...@yahoo.com 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
Jul 19 '08 #2
kk****@yahoo.com 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
Jul 19 '08 #3
On Sat, 19 Jul 2008, kk****@yahoo.com wrote:
/.../
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. But you might want to be careful when X contain a pointer to another
class.

Sincerely,
Peter Jansson
http://www.p-jansson.com/
http://peter.jansson.net/

Jul 19 '08 #4
On Jul 19, 11:25 am, sonison.ja...@gmail.com wrote:
On Jul 19, 7:50 pm, kk_...@yahoo.com 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);

Jul 19 '08 #5
On Jul 20, 1:43 am, puzzlecracker <ironsel2...@gmail.comwrote:
On Jul 19, 11:25 am, sonison.ja...@gmail.com 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:ja*********@gmail.com
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
Jul 20 '08 #6
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.
Sincerely,
Peter Jansson
http://www.p-jansson.com/
http://peter.jansson.net/

Jul 20 '08 #7
On Jul 19, 11:25*am, sonison.ja...@gmail.com wrote:
On Jul 19, 7:50*pm, kk_...@yahoo.com 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
Jul 20 '08 #8
On Jul 20, 12:06 pm, Peter Jansson <webmas...@jansson.netwrote:
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:ja*********@gmail.com
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
Jul 20 '08 #9
On Jul 20, 4:06 pm, kk_...@yahoo.com wrote:
On Jul 19, 11:25 am, sonison.ja...@gmail.com wrote:
On Jul 19, 7:50 pm, kk_...@yahoo.com 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:ja*********@gmail.com
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
Jul 20 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Neil Zanella | last post by:
Hello, I have a file with about 1400 lines of C++ code most of which consists of a single class. An oversimplified version of the file is the following... --- BEGIN ORIGINAL FILE --- ...
2
by: Jacek Dziedzic | last post by:
Is it valid to use a "using namespace foo" (as opposed to using foo::bar which I'm sure is legal) within a class declaration? My compiler rejects it, but I've been told it's valid. Can anyone...
3
by: Dave Rahardja | last post by:
Consider the following example: --- #include <iostream> class Base { public: virtual void fn() { std::cout << "Base::fn()\n"; }
10
by: PengYu.UT | last post by:
Hi, Maybe this is an simple question. How can I insure that the objects of a class must be allocated in the heap? Thanks, Peng
11
by: Chris Dams | last post by:
Dear all, I found out that the program #include<vector> using namespace std; int main() { vector<int*> v(2,0);
9
by: silversurfer2025 | last post by:
Hello everyone, I am currently having problems with a C++ abstract class. I have a class FrameWork.h which defines some methods (of which some are abstract, i.e. virtual void method() = 0). In...
8
by: miaohua1982 | last post by:
the code is as follows: #include<iostream> using namespace std; class A { public: int a; int b;
1
by: subramanian | last post by:
Consider the following program: #include <iostream> #include <string> class Test { public: Test(const std::string &val); private:
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.