tags:

views:

1105

answers:

5

Hello,

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

so like

vector<MyType*>* myvector;

I have to set this pointer to NULL in the constructor and then lazy load when property is touched.

how can i instantiate this to a new instance of a vector ?

thanks -ashish

A: 
myvector = new std::vector<yourtype>;
Nikolai Ruhe
A: 

Well to initialize it you will need to at least set it to NULL or an instance like below using that syntax. Also you should do it in the order the fields are declared otherwise weird stuff might happen.

// Field.
std::vector<int> *myvector;
// Constructor.
myclass() : myvector(new std::vector<int>) 
{ 
}
Skurmedel
+3  A: 

Assuming you define vector correctly:

vector<int>*   myvector;  // Note vector must be parametrized with a type.
                          // There is no such thing as a a 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.

Instantiate on first use:

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

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

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

Martin York
+1  A: 

You cannot haved a pointer like this:

vector* myvector;

because 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 a vector:

myvector = new vector <string>;
anon
+4  A: 

I have to set this pointer to NULL in the constructor and then lazy load when property is touched.

How can i instantiate this to a new instance of a vector ?

I'm not sure i understand you all the way. Why not simply leave the vector empty, and set a boolean that says whether the property was loaded or not? Alternatively, you can use boost::optional

boost::optional< vector<MyType*> >

Or

boost::optional< vector< shared_ptr<MyType> > >

You can then simply receive the object by dereferencing the optional object, and assign a vector to it like usual.

I would not use a pointer for this. It complicates the matter, and you have to think about what happens when you copy the object containing the property, ...

If you really have 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 would be the way to go in my program (but boost::optional would still be first option, after which would be the vector-and-boolean option, after which would 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;         
};

Copy and assignment are disabled, as they would share the prop behind the scene (shallow copy), which should be either clearly documented or disabled by deep copying in these functions.

Johannes Schaub - litb