Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!chalmers.se!appli!niklas From: niklas@appli.se (Niklas Hallqvist) Newsgroups: comp.lang.c++ Subject: I/O problem Summary: How to save an objects binary image on a file? Keywords: C++ I/O binary Message-ID: <1179@appli.se> Date: 3 Nov 90 11:26:27 GMT Organization: Applitron Datasystem AB, GOTHENBURG, SWEDEN Lines: 29 Hello out there... Is there a way to do portable binary I/O on classes with virtual functions? Yes, I know there is, by letting the objects them self provide a binary image via a virtual function. But what I want is to write some- thing like: class A { private: int x; public: virtual int y(void) { return x; }}; void put(A* a) { write(1, &a, sizeof(a)); } A* get(void) { A* a = new A; read(0, &a, sizeof(a)); return a; } And what happens here? Aha, the vtbl pointer gets saved along with the binary image, and will be read in by other programs using the get function. No harm in that, is it? Well, YES!!!! Another program would be differently laid out in memory and the vtbl ptr would not be valid there. Unpredictable results will follow the first virtual call. Now don't tell me you can't do something like this without rewriting all the classes one want to be able to store this way. Summary: How would you do if you had a "black box"- class (i.e no source, just relocatable object code) and wanted to do I/O, keeping the objects full state (even private info)? Thanks, Niklas -- Niklas Hallqvist Phone: +46-(0)31-40 75 00 Applitron Datasystem Fax: +46-(0)31-83 39 50 Molndalsvagen 95 Email: niklas@appli.se S-412 63 GOTEBORG, Sweden mcsun!sunic!chalmers!appli!niklas