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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:44:08+00:00 2026-06-07T01:44:08+00:00

I have a class containing many different variables, for example there’s a few multidimensional

  • 0

I have a class containing many different variables, for example there’s a few multidimensional vectors in there.

I’ve heard that you can store and load data directly to a file, but to what extent?

For example if I create an instance of this class, fill it up, then save it to a file, can I load it the same way? Like how does that work? Do I just save it all in one go or do I have to split up the data somehow?

  • 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-07T01:44:09+00:00Added an answer on June 7, 2026 at 1:44 am

    This is not exactly beginner’s topic in C++

    C++ has no automated way to store / load your objects to / from a file. Either way you chose to go, you’ll have to implement it yourself.

    You might chose to overload the << an >> operators to use with streams, or you might want to go with your own Load and Store methods (or whatever names you chose appropriate, like, Serialize / Deserialize). I personally prefer to create my own functions and not to use the operators, but it’s just me.

    Here is a simple example (with overloaded << and >> operators):

    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    class MyClass
    {
    public:
        MyClass (int x) : m_x(x), m_y(x+1) {}
    
        friend istream& operator >> (istream& in, MyClass& obj);
        friend ostream& operator << (ostream& out, const MyClass& obj);
    
    private:
        int m_x;
        int m_y;
    };
    
    istream& operator >> (istream& in, MyClass& obj)
    {
        in >> obj.m_x;
        in >> obj.m_y;
        return in;
    }
    
    ostream& operator << (ostream& out, const MyClass& obj)
    {
        out << obj.m_x << ' ';
        out << obj.m_y << endl;
        return out;
    }
    
    int main(int argc, char* argv[])
    {
        MyClass myObj(10);
        MyClass other(1);
        cout << myObj;
        ofstream outFile ("serialized.txt");
        outFile << myObj;
        outFile.close();
        ifstream inFile ("serialized.txt");
        inFile >> other;
        inFile.close();
        cout << other;
        return 0;
    }
    

    It worth to mention that you should take care about the serialization format. In the example above it’s just text; but if you are going to store a lot of those objects, you might start thinking about serializing binary data (you’ll need to use ofstream::binary and ifstream:binary flags while opening the files, and will not need additional separators, like ' ' and endl in your serialization stream).

    Typically, when you think of serialization, you also want to think of the versioning of your stream — and this is another separate topic.

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

Sidebar

Related Questions

I have existing C++ lib containing many different classes working together. Some example usage
I have a Shape class containing potentially many vertices, and I was contemplating making
I have a class containing Linq To SQL objects that are used to populate
I have an example class containing two data points: public enum Sort { First,
I have a table containing many rows. Some of these rows are class=highlight and
If I have a class containing many Extension Methods, should I worry about the
I have a JPA entity class (one of many) and I can run JPQL
I have a 2D array containing many instances of a class. The class contains
I have a horizontalscrollview containing many textviews that are added dynamically. When I press
I have a class containing a collection of items. For convenience I've provided GetCurrentItem

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.