2011-05-28 91 views
3

我正在做一個需要在C++中處理文件的任務。我被困在如何編寫,然後回讀動態創建的對象。 i-e通過指向派生類對象的基類指針的幫助。而且我們不知道在哪個特定時間存儲了哪個對象。整個任務足夠長,可以在這裏發佈,但我已經做了一個類似的簡單代碼來詳細說明我的問題。動態對象和文件處理

    #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(); 
     } 
+0

P.S:我試圖簡單地寫入文件並回讀,然後在控制檯窗口中打印。所以我知道我有我的新變量中的對象。 – user773974 2011-05-28 02:49:36

回答

3

如果您想要讀取/寫入任意對象,您要查找的術語是「對象序列化」。

難道C++ serialization的答案對您有幫助嗎?此外,該主題還有一個C++ FAQ entry小節。

+1

我剛剛要鏈接到parashift常見問題解答!我認爲在描述序列化問題方面做得非常好,特別是對於這種情況。 – 2011-05-28 03:02:18