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 4340412
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:20:44+00:00 2026-05-21T11:20:44+00:00

Currently I’m trying to implement some kind of object stream, similar to the structure

  • 0

Currently I’m trying to implement some kind of object stream, similar to the structure that exists in java.

Using the “Stream Socket API” by Rob Tougher it’s easy to use the stream insertion operator<<.

This would lead to the problem that every serializable object would require every single class to overload the same operators in the same manner.

Example:

class A
{
    protected:
        int value;

    friend ostream& operator <<( ostream& in, const A& obj );
};

class B
{
    protected:
        float value;

    friend ostream& operator <<( ostream& in, const B& obj );
};

ofstream& operator<< (ofstream& in, const A& obj)
{
    return (in << obj.value);
}

ofstream& operator<< (ofstream& in, const B& obj)
{
    return (in << obj.value);
}

Well. That’s easy and works. Now I’m thinking about whether it’s somehow possible to ease and automate the process of this serialization.

I already thought back and forth how this could be done using some combination with both, templates and macros. Using typeid (RTTI) would partially work, but this fails at the point where you would need to cast a value in a type when only the typeid is known. Which simply isn’t possible.

Is there really no other solution that to simply let the user implement each and every operator<< ? Am I thinking too far and the solution is quite easy?

Otherwise, in all ways I’ve tried and thought through, the problem ends at where I need to reinstanciate an object when the classname isn’t known at compile-time.

Any idea is appreciated.

PS: Using fwrite(item, sizeof(item), 1, handle); isn’t the solution as well. Not platform independent at all.

  • 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-05-21T11:20:45+00:00Added an answer on May 21, 2026 at 11:20 am

    Java serialization relies on reflection to automatically discover and serialize class fields. In C++, your options are:

    1. Write input and output functions manually for each class. boost::serialization uses this approach.
    2. Declare serializable entities in a separate language and compile that language to C++ with automatically generated in/out operators. CORBA, protobuf, ZeroC ICE and thrift all use this approach.
    3. In theory you could probably use C++ itself as the definition language, but C++ is hard enough to parse that I don’t know of any serialization frameworks that attempt that. Perhaps now that clang can actually compile real programs, someone may try.
    4. Declare every serializable entity using macros and compile every header twice, with different macro definitions (once to declare classes, once to create serializers). I’ve seen it done in closed-source code I had to maintain and the result was extremely ugly and inflexible, so I’d advise against it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I am writing a MiddleWare application that synchronizes information between and accounting application
Currently I am using HTML files for parts of my user interface. I display
currently, I`m implementing a map App with Mono4Droid and there I`m using a WebView
Currently working with converting SQLException error messages into messages that are more useful for
Currently I am trying to use a bunch of custom perl modules, test.pm as
Currently i have a node.js and socket.io application in development on my local machine
Currently I am adding object by creating it like: type TRecord = class private
Currently i'm having trouble using two functions in my -(void)viewDidLoad , both of these
Currently, my MVC 3 app has a dependency on a static class that is
Currently I am using Amazon Cloudfront to service static objects on my ASP.Net MVC3

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.