[Boost-users] [serialization] Private default constructors

56 views
Skip to first unread message

Robert Dailey

unread,
Apr 2, 2009, 8:11:11 PM4/2/09
to boost [usr]
I have a class that can be constructed statically in code, or constructed by data (this is where boost::serialization comes in).

For example:


class foo
{
public:
    foo() {}
    foo( std::string text )
        : m_text( text )
    {}

private:
    template< class Archive >
    void serialize( Archive& archive, unsigned int version )
    {
        archive & m_text;
    }

    std::string m_text;

    friend class boost::serialization::access;
};

There is one problem with this, though. It is possible for a user of this class to create a "foo" with the default constructor, thus enabling them to have "zombie" objects. The only thing I want using the default constructor is boost::serialization, not the user. Is there a way I can make the default constructor private and still have boost::serialization access it? Keep in mind that this must work polymorphically too.

Robert Dailey

unread,
Apr 2, 2009, 8:15:20 PM4/2/09
to boost [usr]
Sorry, apparently this does work as-is.

I was confused about another error message I was getting from the compiler and it mistakenly lead me to believe it was not accessing the private constructor. Sorry for the mix up.
Reply all
Reply to author
Forward
0 new messages