class MyClass {
public:
MyClass(int X= 0; int Y= 0) { /*...*/ }
private:
int x;
int y;
};
I’m trying to avoid the overhead of using boost::serialization library for this simple class. so, is it valid to do something like this:
MyClass Obj(43, 64);
char *c = (char*)&Obj; // ok?
// write to file
Casting your object as a pointer won’t do what you want. It’s valid to cast its address to a pointer to char:
(char*)&Objor better yetreinterpret_cast<char*>( &Obj );. Actually accessing that pointer to store the raw data and later load from it its not guaranteed to work by the standard. The memory layout of complex objects is implementation dependent. It would be standard behavior if the class where a POD type (I think C++11 now calls them Standard Layout Classes).