I am trying to create a constructor to load a resource from any istream given to it. I cannot seem to figure out the best way to pass the istream parameter into a constructor.
Loader::Loader(istream stream);
This one is obviosly bad due to object slicing, so no option.
Loader::Loader(istream& stream);
This is what I am using now and seems fairly alright. It has one significant issue though - you can't give it a temporary since temporaries cannot bind to non-const references! For example, the following won't work:
Container():
mLoader(ifstream("path/file.txt", ios::binary)
{
}
This is rather a limitation since I am now forced to store the ifstream as a member variable of Container just to extend its lifetime.
Since the problem is with non-const references, one could have though of this:
Loader::Loader(const istream& stream);
But since .seek() etc are non-const, this is not an option either...
So, how can this problem be solved in a neat way?