Thread: Reinterpret Cast to char* for Serialization?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    Reinterpret Cast to char* for Serialization?

    Some of my coursework material suggests using a reinterpret cast:
    Code:
    outfile.write(reinterpret_cast <char*> (&Object_name), sizeof (Object_type) );
    To serialize an object. Is this as bad of an idea as I think it is?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Is this as bad of an idea as I think it is?
    Absolutely. How well would that work for this object:

    Code:
    class WontSerialize
    {
        WontSerialize(void) { pBuffer = new char[100]; }
        char* pBuffer;
    };
    It is way better to let objects serialize themselves. For instance, it is common to serialize objects as XML these days, so you could make your class have a ToXML() function.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    there exist bosst::serialisation and it is able to serialize to xml, binary and text.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    reinterpret_cast is normally a bad idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reinterpret vs c-style cast
    By @nthony in forum C++ Programming
    Replies: 61
    Last Post: 11-23-2008, 03:47 AM
  2. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 03:32 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM