Well, firstly, the operator ==
is used for comparison, not assignment. For assignment, you want a single =
. Secondly, your code is not portable, and possibly broken, because the way your object is stored on disk as a sequence of bytes is not necessarily the same way that it is stored in memory as a T
object. This is because different computers/platforms/compilers represent binary data in different ways. Plus, as Vlad mentions in the comment below, if instances of T
contain internal pointers, (like std::string
), then your program will simply fall apart.
You should probably look into a serialization library, or at least use C++ iostreams to serialize your object into a text format, then use an istream_iterator
to read them from disk.