c++ - how to reuse class containing std::ifstream? -


i have class handles file opening, reading , closing. have class b use read file. b has instance of private member data. want reuse a , use read multiple files using instance. read cannot copy of stream. question how can handle class read multiple files in b?

class a{    a(std::string s){       f.open(s);    }    void read_file(){         /// read file          // close after reading         f.close();     } private:    std::ifstream f; };  class b{    b(std::string s_):a(s_){}     void read_multiple_files(){        a.read_file();         // lets read file        = a("another_file_1.txt");        a.read_file();        ////////////////////         // lets read file        = a("another_file_2.txt");        a.read_file();    } private:     a }; 

this design issue. there seems no reason b hold instance of a unless needs keep file handle across different method calls.

instead, create a read each file:

class b {    void read_multiple_files() {         // read our files        auto result = a("another_file_1.txt").read_file();        auto result_2 = a("another_file_2.txt").read_file();        ...    } } 

Comments