C++/transient and Serialization
Is there Serialization in C++? In java, there is Serialization, and the keword word "transient" is used to specify that some members of a class can not be serialized, what does the keword word "transient" do in C++?
You could answer this yourself by just trying it!
You would find that transient is _not_ a keyword in C++. C++ has no native support for serialisation. This follows from the differences in approach to the type systems of the likes of Java and .NET and C++.
C++ tries to do as much as it can with types (and everything else) during compilation. That is it is a statically typed system. What run time (i.e. dynamic) support there is for the type system is very limited - just about enough to allow dynamic_cast to perform cross casts when multiple inheritance is in use.
Java and .NET on the other hand are built on top of their own runtime systems - the Java Virtual Machine (JVM) for Java and the Common Language Infrastructure (or CLI) for .NET, of which the Microsoft commercial implementation is called the CLR (Common Language Runtime). This allows them to support a much more dynamic type system. One of the things you can do in these systems at runtime is reflection - find out a lot about what your objects and types are made up of. You can also create types at runtime, and even emit code (well you can in .NET, I do not know about Java). These facilities are just the sort of things you might require to help in performing tasks such as serialisation of objects automatically. You find out about the internals of the object, and from there about the members of the object type. You can then create member:value pairs for each member that requires data saved. Of course then you have to have some facility to mark members that should not be saved during serialisation, hence the transient keyword.
C++ on the other hand tries to minimize the cost in space and/or time of any feature. In particular it tries to achieve a zero overhead for any feature you are not using. That is if you are not using it you do not need to pay for it.
Because C++ is predominately statically typed most information about the types you create and use is thrown away by the compiler. This comes from C++'s focus on efficiency. Most features in C++ require very little support at runtime, other than the provision of a heap (for new/delete) and the start up code to get command line parameters into main correctly. Even the requirement for main is not required for a freestanding implementation (one that has minimal library support, only 6 of the usual C++ library headers are required). Hence the code produced can be very efficient. The exceptions are exceptions (<g>) and runtime type information required for dynamic_cast and typeinfo. You will often see compiler options to turn support for these features off so if you are not using them you incur no overhead.
Remember C++ compilers generally compile to native machine code, not Java byte code or MSIL (Microsoft Intermediate Language) as Java and .NET compilers do. (OK some compile to C or assembler code and then run a C compiler or assembler to produce a native machine code object file). On the one hand this allows C++ (like C) to be used for systems and embedded applications on systems without an operating system. On the other hand it means you have to re-compile application code for each platform. One of the stated boons of Java, if I remember correctly, was that you compiled once and ran the application everywhere (so long as everywhere had an installed compatible JVM of course!).
To add serialisation to a C++ application requires work. However there are several serialization support implementations around. A good starting point would be the Boost (http://www.boost.org/
) serialisation library. The site is currently down (at least I cannot access it!), but a Google search for boost serialization produces a link to www.boost.org/libs/serialization/index.html. If this is incorrect then if you click the documentation link under libraries in the top right of the home page and select Serialization from the list of Boost libraries it should get you there. You generally have to define a function (member or free standing) to write or read the data members of the type in question to or from some archive object.
The only other alternative I can see is to concoct some sort of enhanced runtime type information system for use with C++ programs, and then build serialisation on top of this. There would still be the problem of differentiating members to be serialised and members not to be serialised! Dr. Bjarne Stroustrup did do some work in this area, see http://lcgapp.cern.ch/project/architecture/XTI_accu.pdf
. I suppose such a system could be designed to allow the addition of transient/persistent markers.