I need to set this pointer to NULL in the constructor, and then to lazy load when the property is touched.
How can I create an instance of this instance for a new instance of the vector?
I'm not sure I understand you all the way. Why not just leave the vector blank and set a boolean value indicating whether the property is loaded or not? Alternatively, you can use boost::optional
boost::optional< vector<MyType*> >
Or
boost::optional< vector< shared_ptr<MyType> > >
Then you can simply get the object by dereferencing the optional object and assign it a vector, as usual.
I would not use a pointer for this. This complicates this question, and you should think about what happens when you copy an object containing a property, ...
If you really need to use a pointer, you can do it like this:
struct A { A():prop() { } ~A() { delete prop; } vector< MyType *>& get() { if(!prop) prop = new vector< MyType* >(); return prop; } private:
Or use shared_ptr , which will be the way to go to my program (but boost :: optional will still be the first option, after which there will be a vector-logical option, after which there will be the following)
struct A { typedef vector< shared_ptr<MyType> > vector_type; vector_type &get() { if(!prop) { prop.reset(new vector_type); } return *prop; } private:
Copying and assignment are disabled, as they will share the footing behind the scene (shallow copy), which should be clearly documented or disabled by deep copying in these functions.