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

  • Home
  • SEARCH
  • 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 806917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:19:14+00:00 2026-05-15T00:19:14+00:00

I am trying to create a generic class to write and read Objects to/from

  • 0

I am trying to create a generic class to write and read Objects to/from file.
Called it ActiveRecord class

only has one method, which saves the class itself:

void ActiveRecord::saveRecord(){
 string fileName = "data.dat";

 ofstream stream(fileName.c_str(), ios::out);
 if (!stream) {
  cerr << "Error opening file: " << fileName << endl;
  exit(1);
 }
 stream.write(reinterpret_cast<const char *> (this), sizeof(ActiveRecord));
 stream.close();
}

now I’m extending this class with User class:

class User : public ActiveRecord
{
 public:
  User(void);
  ~User(void);
  string name;
  string lastName;
};

to create and save the user I would like to do something like:

User user = User();
user.name = "John";
user.lastName = "Smith"
user.save();

how can I get this ActiveRecord::saveRecord() method to take any object, and class definition so it writes whatever i send it:

to look like:

void ActiveRecord::saveRecord(foo_instance, FooClass){
 string fileName = "data.dat";

 ofstream stream(fileName.c_str(), ios::out);
 if (!stream) {
  cerr << "Error opening file: " << fileName << endl;
  exit(1);
 }
 stream.write(reinterpret_cast<const char *> (foo_instance), sizeof(FooClass));
 stream.close();
}

and while we’re at it, what is the default Object type in c++.
eg.
in objective-c it’s id
in java it’s Object
in AS3 it’s Object
what is it in C++??

  • 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-15T00:19:15+00:00Added an answer on May 15, 2026 at 12:19 am
    stream.write(reinterpret_cast<const char *> (foo_instance), sizeof(FooClass));
    

    This doesn’t work. string allocates its data on the heap (IIRC, when it’s larger than 16chars). Your reinterpret cast will not include that heap data.

    Don’t reinvent the wheel, this is a non-trivial, but solved problem. Use Google Protocol Buffers, XML, or the boost serialization library.

    What you have in mind is templates, but you can’t “just serialize any object” because outside of POD (plain-old-data) types, their representation isn’t obvious.

    In addition, using sizeof(BaseClass) with the User subclass will not work. It’ll slice the subclass’s member data off of the cast and not put it in the file.

    And there is no “default Object type in c++”.

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

Sidebar

Ask A Question

Stats

  • Questions 423k
  • Answers 423k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is basically eaten by a third-party class that I'll… May 15, 2026 at 11:32 am
  • Editorial Team
    Editorial Team added an answer One thing you should try to ensure with any regular… May 15, 2026 at 11:32 am
  • Editorial Team
    Editorial Team added an answer No, the link assigned to the containing <a> will be… May 15, 2026 at 11:32 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.