Advanced Tools Module User’s Guide : PART II Advanced Tools Module Packages : Chapter 6 Using Object Serialization : Basic Examples : Default Constructors
Default Constructors
Reading objects from object streams using polymorphic pointers requires an object factory that can use new() to create an object of the correct derived type. This factory in turn needs access to the default constructor (no arguments) for each possible derived type.
This is not a problem if the default constructor is public, but when the result of calling the default constructor is an incomplete object, a protected or private constructor is desired. The RW_DECLARE_FRIEND_CTOR_METHOD() macro is included for this situation. After the call to RW_DECLARE_VIRTUAL_STREAM_FNS() within the class declaration, a call to RW_DECLARE_FRIEND_CTOR_METHOD() must be made for each base class whose factory calls this class’s default constructor. The following is the header for residential after making the default constructor private.
 
// examples\serial\other\residential.h
 
class residential : public real_property
{
RW_DECLARE_VIRTUAL_STREAM_FNS(residential)
RW_DECLARE_FRIEND_CTOR_METHOD(real_property,residential) // 1
 
residential (); // 2
 
public:
...
};
 
// Allow object streaming of residential class
RW_DECLARE_STREAMABLE_AS_SELF(residential)
RW_DECLARE_STREAMABLE_POINTER(residential)
//1 Use of the macro to declare the factory a friend.
//2 Note that the default constructor is private.
You cannot add a friend to an existing class without modifying its header file. Therefore when using a third party library, an instance must be created using a public constructor.