np-hard
np-hard

Reputation: 5815

Instantiate a new STL vector

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 the property is touched.

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

Upvotes: 4

Views: 45308

Answers (5)

anon
anon

Reputation:

You cannot have 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>;

Upvotes: 1

Loki Astari
Loki Astari

Reputation: 264381

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 a 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.

Upvotes: 7

Skurmedel
Skurmedel

Reputation: 22149

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>)
{
}

Upvotes: 0

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506905

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 property behind the scene (shallow copy), which should be either clearly documented or disabled by deep copying in these functions.

Upvotes: 5

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81858

myvector = new std::vector<yourtype>;

Upvotes: 1

Related Questions