Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8661781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:34:50+00:00 2026-06-12T16:34:50+00:00

Is there a way to serialize a class in C++? I am not referring

  • 0

Is there a way to serialize a class in C++? I am not referring to an instance of a class but the class itself. When deserializing I do not know what class to instantiate to call deserialize on it. I want to be able to somehow determine what class is being represented, instantiate an empty instance of it, and then call deserialize it.

Edit for clarification:

I am creating a key-value structure where each key has a strongly typed value. (Some keys must store a string, others must store ints, and still others may store arbitrary values). I have devised a system where this is enforced by the compiler through templates.

The Key class has two template parameters:

  • UniqueClass – There should be one unique class per concept (usually defined in a Preprocessor function that defines a concept)
  • ValueType – This is the class that the value must be for this concept

The Value class is a templated class with just the ValueType that has an abstract superclass so that I can wrap any class within it (similar to a boost::any).

Then in my Key-Value structure I have store and retrieve functions that are templated with a ConceptType and a ValueType:

template<UniqueClass, ValueType>
void store( Concept<UniqueClass, ValueType> concept, ValueType value );

Because I am using the same template to describe the value type of the Concept and the passed in value the compiler will complain if they don’t match.

I now want to be able to serialize the Key-Value store. Therefore I need to somehow serialize the templated Concept class and then instantiate the correct arbitrary class during deserialization.

I have it so that I can call serialize and all the appropriate objects are asked to serialize themselves, I just don’t know what class to instantiate during deserialize to ask to deserialize itself.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:34:51+00:00Added an answer on June 12, 2026 at 4:34 pm

    Answer updated due to question edit:

    What I understand is that you have 0 keys in the container until you add it manually or via deserialization. There are two way for doing this: intrusive and non intrusive.

    Intrusive

    Let Concept<U,T> inherit from Deserializable:

    class Deserializable
    {
        public:
            virtual void deserialize(InputStream& s) = 0;
            virtual int getClassId() const = 0;
    };
    

    Now, you can identify a class via its id, retrieved using getClassId(). So, next is to create a factory. Here I will use a really simple one, with std::map, to show the point; feel free to use a more sophisticated one.

    typedef Deserializable* (*createDeserializableType)();
    std::map<int,createDeserializableType> factory;
    
    // ...
    
    factory[id1] = &createMyClass;
    // etc...
    
    // ...
    
    // On deserialization
    createDeserializable create = factory[idReadFromStream];
    if(create)
        create()->deserialize(input_stream);
    

    It means either you have to add a store method for Deserializable or you add a store method to Deserializable (I personally prefer the last method).

    Non-intrusive

    The idea is to deserialize the key-value the same time you are creating it and adding it:

    typedef void (*deserializeFctType)(InputStream&);
    typedef int id_type;
    std::map<id_type,deserializeFctType> factory;
    

    The deserialize method will store the deserialized type immediately after creating it.

    What is not clear in your question is whether the list of accepted list is finite or infinite. Only a finite set of type is “easily” serializable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a class I want to serialize, and it implements Serializable, but one
Is there a way to cause XmlSerializer to serialize primitive class members (e.g. string
Is there a simple way to serialize a single-level structure as a string for
I want to XML-Serialize a complex type (class), that has a property of type
I have a class with a map, and I want to serialize the class
Is there a way to serialize a lexical closure in Python using the standard
Is there any way to de/serialize an object without round-tripping a XmlDocument/temp string? I
Related to: Is there a way to obtain the bytecode for a class at
Is there any way to add fields to a DataSet which will be serialized
Is there way to get file from windows xp command prompt? I tried to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.