Reinterpret Cast to char* for Serialization?

This is a discussion on Reinterpret Cast to char* for Serialization? within the C++ Programming forums, part of the General Programming Boards category; Some of my coursework material suggests using a reinterpret cast: Code: outfile.write(reinterpret_cast <char*> (&Object_name), sizeof (Object_type) ); To serialize an ...

  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,246
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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, 02:47 AM
  2. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 02: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, 09:14 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 05:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21