c++ - 动态对象和文件处理

标签 c++ object dynamic file-io

我正在做一项需要在 C++ 中处理文件的作业。我被困在如何编写然后读回动态创建的对象上。即借助指向派生类对象的基类指针。而且我们不知道在任何特定时间哪个对象存储在那里。整个作业足够长,可以在这里发布,但我已经编写了一个类似的简单代码来详细说明我的问题。

                    #include <string>
    #include <conio.h>
    #include <iostream>
    #include <vector>
    #include <fstream>

    using namespace std;

    class MAss
    {
    private:

    public:
        int age;
        string name;

        MAss();
        virtual void print();

         friend ofstream & operator << (ofstream & out, MAss & obj);
         friend ifstream & operator >> (ifstream & in, MAss & obj);
    };


    void MAss :: print()
    {
        cout<<endl<<"age = " << age;
        cout<<endl;
        cout<<"name = " <<name;
    }


    MAss :: MAss()
            :age(10)
            , name("Random")
        {}

    ofstream & operator <<  (ofstream & out, MAss & obj)
    {
        out<<obj.age;
        out<<" | " <<obj.name<<endl;
        return out;
    }


    ifstream & operator >> (ifstream & in, MAss & obj)
    {
        string temp;
        in>>obj.age;
        in>>temp;
        in>>obj.name;
        return in;
    }





    class Bass : public MAss
    {
    public:

        int reg;
        Bass();

        void print();

         friend ofstream & operator << (ofstream & out, Bass & obj);
        friend ifstream & operator >> (ifstream & in, Bass & obj);

    };

    Bass ::Bass()
        : reg(1)
    {}


    void Bass :: print()
    {
        MAss ::print();
        cout<<endl<<"REG = " <<reg;
    }

    ofstream& operator<<(ofstream& f, Bass& obj)
    {
        MAss & a = obj;

        f<<obj.reg<<" | ";

        f<<a;
        return f;
    }



    ifstream& operator>>(ifstream& f, Bass& obj)
    {
        string temp;
        MAss & a = obj;

        f>>obj.reg;
        f>> temp;
        f>>a;
        return f;
    }           




        void main ()
        {
            vector <MAss*> b(6);
            cout<<endl<<b.size();
            ofstream o("E:\\A.txt");

            for(int i=0; i<b.size();i++)
                b[i] = new Bass;

            b[0][0].age =1;
            b[0][0].name = "Jack";

            b[1][0].age =2;
            b[1][0].name = "Jill";


            b[2][0].age =3;
            b[2][0].name = "Jane";

            b[3][0].age =1;
            b[3][0].name = "Tom";


            b[4][0].age =2;
            b[4][0].name = "Phill";


            b[5][0].age =3;
            b[5][0].name = "Bone";


            for (int i=0; i<b.size(); i++)
            {
                o << b[i][0];
            }

            o.close();



            ifstream in("E:\\A.txt");
            vector <MAss*> arr;
            while(!in.eof())
            {
                Bass a;
                in >> a;

                a.print();
            }

            arr.pop_back();

            cout<<endl<<arr.size()<<endl;


            for (int i=0; i<arr.size(); i++)
            {
                cout<<endl<<endl<<endl<<endl<<"INDEX : " << i<<endl;
                arr[i][0].print();
            }



            getch();

            in.close();
        }

最佳答案

如果你想读/写任意对象,你正在寻找的术语是“对象序列化”。

C++ serialization 的答案帮你?此外,还有一个 C++ FAQ entry关于该主题的小节。

关于c++ - 动态对象和文件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6159366/

相关文章:

javascript - 为什么我的 javascript 对象的值未定义?

dynamic - 如何动态创建捕获 (Raku)

c++ - 必须具有通用接口(interface)但必须根据传递的子类表现不同的函数(不知道它们是什么!)- C++

c++ - 线程的子线程?

javascript - Shopify - 我可以从浏览器控制台访问 JSON 对象,但不能从主题文件访问

c++ - 成员到指针变量仅在带有-std = c++ 17的Clang中被接受为函数模板参数

c++ - 打印对象数组的每个元素时出现问题

c++ - 如何从运算符函数返回动态对象?

c++ - 需要选择一个容器来存储我的数据

c++ - C++ 中成员函数的 const& 、 & 和 && 说明符