Create a new stl vector

I have a situation where I have a pointer to stl vector

so that

vector<MyType*>* myvector; 

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 a new instance of a vector?

+4
source share
5 answers

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: // disable copy and assignment. A(A const&); A& operator=(A const&); vector< MyType* > *prop; }; 

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: // disable copy and assignment. A(A const&); A& operator=(A const&); shared_ptr< vector_type > prop; }; 

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.

+5
source

Assuming you are defining a vector correctly:

 vector<int>* myvector; // Note vector must be parametrized with a type. // There is no such thing as aa naked vector. 

Initialize to NULL

 myclass::myclass() :myvector(NULL) // You can use 0 here but I still like NULL because it {} // gives me more information. Waiting for the new keyword. 

Instant on first use:

 myvectr = new vector<int>(100); // reserve some space as appropriate 

But you should not have a RAW pointer as a member of your class (unless there is a very good reason). You will need to write your own copy constructor and assignment operator.

Or you can wrap "myvector" with a smart pointer. Or even better to make it a normal vector. There is no real need to make it a pointer.

+7
source
 myvector = new std::vector<yourtype>; 
+1
source

You cannot specify a pointer like this:

 vector* myvector; 

because the vector is a template class and must have a type. You could say:

 vector <int> * myvector = 0; 

or

 vector <string> * myvector = 0; 

and then dynamically create the vector:

 myvector = new vector <string>; 
+1
source

To initialize it, you need to at least set it to NULL or an instance, as shown below, using this syntax. You must also do this in the order in which the fields are declared, otherwise strange things may happen.

 // Field. std::vector<int> *myvector; // Constructor. myclass() : myvector(new std::vector<int>) { } 
0
source

Source: https://habr.com/ru/post/1285969/


All Articles