mardi 4 octobre 2016

How to use c++11 feature of class member initializer to initialize ifstream with error check?

I am trying to use C++11 feature of class member initializer to initialize variables of class. The variables of class that I have are std::string and std::ifstream.

class A{
    std::string filename = "f1.txt";
    std::ifstream filestream = ....
public:
    ....
};

Is there any way to initialize filestream and also check for error at the same time using class member initialization.

What I want to do is, something similar to below :

class A{
    std::string filename = "f1.txt";
    std::ifstream filestream(filename);
    if(filestream.is_open()) .... // check if file cannot be opened
public:
    ....
};

Aucun commentaire:

Enregistrer un commentaire