Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在类的构造函数中初始化ifstream变量_C++_Constructor_Ifstream - Fatal编程技术网

C++ 在类的构造函数中初始化ifstream变量

C++ 在类的构造函数中初始化ifstream变量,c++,constructor,ifstream,C++,Constructor,Ifstream,如何在类的构造函数中初始化ifstream变量? 下面给出的是类和构造函数。我正在使用初始化列表初始化ifstream变量。下面的代码可以工作 class A { public: A(ifstream& in1); // constructor private: ifstream& input; // reference to the input stream }; A::A(ifstream& in1) :input(in1) { //counstructor

如何在类的构造函数中初始化ifstream变量? 下面给出的是类和构造函数。我正在使用初始化列表初始化ifstream变量。下面的代码可以工作

class A
{
public:
A(ifstream& in1);  // constructor

private:
ifstream& input;  // reference to the input stream
};

A::A(ifstream& in1) :input(in1)
{
//counstructor used to initialise the member variables. Initialization list     used to initialize.
}
为什么下面的代码不起作用

A::A(ifstream& in1) 
{
input=in;
}

引用必须在声明时初始化

A::A(ifstream& in1) :input(in1)
这将在声明时初始化它。成员初始值设定项列表是初始化构造函数中引用的方法

input=in;

这不正确。

引用必须在声明时初始化

A::A(ifstream& in1) :input(in1)
这将在声明时初始化它。成员初始值设定项列表是初始化构造函数中引用的方法

input=in;

这不正确。

引用不能用赋值初始化。一旦它被初始化,它只是它所指的任何东西的别名。对它的赋值是对它所引用内容的赋值,而
std::ifstream
是不可复制的。

引用不能用赋值初始化。一旦它被初始化,它只是它所指的任何东西的别名。对它的赋值就是对它所指内容的赋值,而
std::ifstream
是不可复制的