Reputation: 3658
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
Upvotes: 0
Views: 124
Reputation: 81399
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*)&Obj
or better yet reinterpret_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).
Upvotes: 2