473,395 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Serialization template base class

Hello,
I need help with the design of a serialization template base class.
The idea is that any class derived from this template base class would only need to implement the input and output operators and a named constructor, Create().
The code is something like this:

Expand|Select|Wrap|Line Numbers
  1. template <class SerializableClass>
  2. class Serializable
  3. {
  4. public:
  5.     static SerializableClass* Unserialize(std::istream& in){
  6.         SerializableClass* instance = SerializableClass::Create();
  7.         in >> *instance;
  8.         return instance;
  9.     }
  10.  
  11.     std::ostream& Serialize(std::ostream& out){
  12.         out << *this;
  13.         return out;
  14.     }
  15.  
  16. protected:
  17.     static Create();
  18.  
  19. };
As I know, the input and output operators must be friend functions to the type they provide I/O for, so they cannot be member functions. But then I can't promise the compiler that the operators will be implemented for SerializableClass, so this doesn't compile.
Is there a way to make this design work?
Also, I think I miss some point of either the I/O operators or design with templates, so help with that is also appreciated.
May 19 '08 #1
5 3148
arnaudk
424 256MB
By i/o operators, do you mean Serialize/Unserialize methods? Because you haven't written them as operators.
What about making the i/o methods of your base class pure virtual? You can still provide a base class implementation for them, but this will force them to be implemented in derived classes. You can call the base class implementations of the pure virtual methods from within derived classes if need be.
May 19 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Actually, I believe you want to implement a Visitor.

Read this article: http://bytes.com/forum/thread674645.html.

How to serialize a class is covered towards the end of the article.
May 19 '08 #3
By i/o operators, do you mean Serialize/Unserialize methods? Because you haven't written them as operators.
By I/O operators I mean std::istream& operator >> (std::istream&, <class>&) and std::ostream& operator << (std::ostream&, <class>&), which are non-member friend functions.
What about making the i/o methods of your base class pure virtual? You can still provide a base class implementation for them, but this will force them to be implemented in derived classes. You can call the base class implementations of the pure virtual methods from within derived classes if need be.
How can a base implementation be provided for a pure virtual method?
Actually, I believe you want to implement a Visitor.

Read this article: http://bytes.com/forum/thread674645.html.

How to serialize a class is covered towards the end of the article.
Thanks weaknessforcats, the Visitor pattern really is what I wanted to achieve.
May 20 '08 #4
arnaudk
424 256MB
How can a base implementation be provided for a pure virtual method?
http://bytes.com/forum/thread796389.html
May 20 '08 #5
http://bytes.com/forum/thread796389.html
Thanks arnaudk, that cleared my confusion.
May 21 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
0
by: okinrus | last post by:
Can someone take a look at this code and figure out why Serializable_base::add_serializer throws std::bad_alloc. The problem seems to be the compiler because msvc++ 7.1 says...
4
by: ron | last post by:
Hi, I have class object that i serialize using the System.Xml.Serialization class. Intermittently the object is not getting serialized correctly, using System.Xml.Serialization classes....
2
by: ofer | last post by:
Hi, I am working with the beta version of the new .net framework (Whidbey) and I encountered a problem with serialization that did'nt exist in the .net 2003 the situation is like this : I have...
4
by: Alexander | last post by:
There was such problem, At compilation with SP1 (dotnetfx11SP1) On computers without him{it} недесериализуются some objects. For example (System. Collections. Comparer) and together with...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
6
by: John Glover | last post by:
I'm having a very strange problem with XML serialization. I'm writing web services which pass instances of various classes back and forth as parameters and return values of web methods. The...
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
1
by: MindWrapper | last post by:
boost serialization of polymorph classes from DLLs Folks, Let's consider following code -------------------------------- // base.h // abstract base class class IBase {
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.