c ++ std :: ifstream构造函数:为什么构造函数使用单个参数解析以及为什么它无法使用c ++样式实例化对象

问题描述

我想将文件保留为以下课程的成员:

class MyClass
    {
    public:
        MyClass();
        ~MyClass();

    private:
        // why this line is OK   
        std::ifstream fileOK = ifstream("..\\abc.txt");
        // why this line is NOT OK  
        std::ifstream fileNotOK("..\\abc.txt");

    };

但是,如果我在任何成员函数中写入相同的错误行,那么它将起作用:

MyClass::MyClass()  {
        std::ifstream fileNotOK("..\\abc.txt");
    }

另外,我注意到,没有std::ifstream的构造函数只有一个参数,例如:ifstream (const char* filename)

所以单参数构造函数的工作原理以及为什么不能从类声明中实例化像c ++这样的成员文件对象。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)