I have a quick question about encapsulating specific types with typedef. Say I have a class Foo whose constructor takes a certain value, but I want to hide the specific type using typedef:
class Foo {
public:
typedef boost::shared_ptr< std::vector<int> > value_type;
Foo(value_type val) : val_(val) {}
private:
value_type val_;
};
But in this case, the main function still has to know the type (so it’s explicitly using std::vector<int>):
int main() {
Foo::value_type val(new std::vector<int>());
val->push_back(123);
Foo foo(val);
return 0;
}
How can I fix that while still avoiding a deep copy of the vector in the Foo constructor?
Various solutions:
However, if all you want is to have a vector member in Foo initialized from outside data without copying it, instead of actually sharing through a shared_ptr, then I wouldn’t use a shared_ptr at all. Take a reference in Foo’s ctor and document that it changes the object.