4

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?

3
  • It's i little more C++'ish to set the myvector to 0 instead on NULL. Jun 9, 2009 at 14:54
  • @litb how do you know it's a vector of pointers? this just obfuscates things, IMHO
    – anon
    Jun 9, 2009 at 14:59
  • @Neil, i didn't know. I looked in his question and found he didn't format correctly ( <MyType*> was taken as a html tag ) :) Jun 9, 2009 at 15:22

5 Answers 5

7

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.

5

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.

1
myvector = new std::vector<yourtype>;
1

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>;
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.