struct vector for path, file, and stream?

I have project that is passing around a lot of path, file, and stream arguments.
So maybe a struct vector can contain all that information, and reference the vector where ever a path argument is needed.
1
2
3
4
5
6
7
8
9
struct Path
{
    const std::string path;
    const std::string file;
    const std::iostream& stream;
};
std::vector<Path> vPaths;
//vPaths is initialized in the constructor
//example usage: vPaths[4].stream 

Is there a name for this sort of thing so I can find a pattern or example?
Or is vPaths a bad idea?

Thank you.
Whenever you have a set of related variables a structure of a class can be considered, however watch out about creating a "bunch" of open stream instances since there may be limitations on the number of allowed open files on your system. And what happens if opening one or several of the streams fails, how and where are you going to handle the error.

Why do need to pass both the strings containing the paths and file names along with the stream instances. Perhaps you should just be passing one or the other into your functions?

I know it is just an example, but why the const qualifications? And why the stream reference?
Why do need to pass both the strings containing the paths and file names along with the stream instances?

The streams are for input from files and output to files.
A path and file name are printed to the file to document where the data came from.

why the const qualifications?

The values don't change.

why the stream reference?

That's how I have been defining ofstreams in functions.
The values don't change.

Then make them private and only allow the constructors to modify the values.

That's how I have been defining ofstreams in functions.

But that's not how you should be defining them in your class. The streams in your class should be normal, non-const and non-reference instances. By the way in your functions you really should be passing things like std::string by reference/const reference because copying can be quite expensive when using non-POD types.

By the way you really didn't answer one of the more important questions:
And what happens if opening one or several of the streams fails, how and where are you going to handle the error.



Last edited on
And what happens if opening one or several of the streams fails, how and where are you going to handle the error.

I am going to have to look into how to do that. Maybe I will open and close the streams so only one or two at a time are open.

Thanks for your help jlb.
Last edited on
Maybe I will open and close the streams so only one or two at a time are open.

Then you probably shouldn't have the stream in the structure.

OK, that's what I needed to know. I would have taken me a long time to figure that out.
Thank you so much jlb.
Topic archived. No new replies allowed.