This is a pretty basic C++ design question:
I have a class that contains some data which is read-only, once the object is constructed:
class Foo {
private:
class Impl;
Impl* impl_;
public:
int get(int i); // access internal data elements
};
Now, I’d like to implement several ways to construct a Foo object and fill it with data: from std::istream, from an iterator, a vector, etc. What’s the best way to implement that?
I could just add all those constructors directly in Foo, but I don’t really want the Foo user to have to include std::istream etc. I’m also worried about classes containing too much code.
What is the most idiomatic way to do this? I guess, add some private addElement function, and then define friend factory functions that create Foo objects by reading data, calling addElement and return the constructed object? Any other options?
If you want to construct something from a range, perhaps:
Usage: