コンストラクターで istream 参照を受け入れるクラスがあります。コンストラクターに一時的なオブジェクトが渡された場合、myclass obj(ifstream("filename"));
その ifstream の寿命は長くなりobj
ますか? クラス内の参照またはポインタに割り当てられているかどうかに依存しますか?
例えば:
class test
{
public:
istream *p;
test(istream &is)
{
p = &is;
cout << "a constructor" << endl;
}
~test()
{
cout << "a destructor" << endl;
}
bool isgood()
{
return p->good();
}
};
int main()
{
test test(ifstream("test.cpp"));
cout << test.isgood() << endl;
}
出力:
a constructor
1
a destructor
出力がファイルが良好であると言っているからといって、それが破壊されたのか、それとも何なのかわかりません。これをカバーする規格の一部があれば、私に知らせてください。ありがとう