C
Clint Ruen
Hello all,
I have written out a data structure using the binary flag on an
ofstream. The struct/class is something like this
class SomeData
{
public:
int data1;
float data 2;
}
the call was something like this
fout.write( ( char* )( &theData), sizeof( theData ) );
Now. when I read this back in with an ifstream...
fin.read( (char*)( &newData) , sizeof( newData ) );
I can fill up these classes/structs. However, when I make the class
virtual( by adding a virtual function ), the write function stores the
pionter to the vtable as the first four bytes, then the consecutive
member data.
When I read the data back in, the data seems to be read in properly,
ignoring those bytes that store the pointer to the vtable, and update
the vtable pointers correctly.
Is this behaviour correct? Or am I dancing with undefined behaviour
and getting lucky? Is there an RTFM for this? I guess that the
ifstream knows that the class it's reading in is a virtual class,
hence ignoring that pointer, then filling it in with correct data???
Is this a compiler implementation, is it standard? help, I can't
believe this works but it does ????
--clint
I have written out a data structure using the binary flag on an
ofstream. The struct/class is something like this
class SomeData
{
public:
int data1;
float data 2;
}
the call was something like this
fout.write( ( char* )( &theData), sizeof( theData ) );
Now. when I read this back in with an ifstream...
fin.read( (char*)( &newData) , sizeof( newData ) );
I can fill up these classes/structs. However, when I make the class
virtual( by adding a virtual function ), the write function stores the
pionter to the vtable as the first four bytes, then the consecutive
member data.
When I read the data back in, the data seems to be read in properly,
ignoring those bytes that store the pointer to the vtable, and update
the vtable pointers correctly.
Is this behaviour correct? Or am I dancing with undefined behaviour
and getting lucky? Is there an RTFM for this? I guess that the
ifstream knows that the class it's reading in is a virtual class,
hence ignoring that pointer, then filling it in with correct data???
Is this a compiler implementation, is it standard? help, I can't
believe this works but it does ????
--clint