I have a class that accepts an istream reference in the constructor. If the constructor is passed a temporary object like myclass obj(ifstream("filename"));
will that ifstream be good for the life of obj
? Does it depend on whether or not it is assigned to a reference or pointer in the class?
For example:
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;
}
Output:
a constructor
1
a destructor
Just because the output says the file is good I don't know if it's been destroyed or what. If there is a part of the standard that covers this please let me know. Thanks