Maverik
Maverik

Reputation: 2408

Pointer and vectors in C++

I'm starting with C++ and I have a doubts:

I'm making a function which will return a vector of objects of a class MyClass.

vector<MyClass>* myMethod()

The first question is, it's correct to return a pointer?

The second question is: if I'm going to return a pointer, should I also insert pointer of MyClass object into the vector?

MyClass* object;
myVector.push_back(*object);

Upvotes: 4

Views: 20720

Answers (4)

LihO
LihO

Reputation: 42133

There is nothing wrong with method that returns pointer to vector like this: vector<MyClass>* myMethod(). But you should ask yourself some questions, like:

  1. Does it have to be pointer to vector? Can't it be just vector<MyClass> ?
  2. Should this method also allocate memory for this vector?
  3. Should caller of this method delete / free this memory ?

And to your second question: I would make vector of pointers to objects of MyClass (vector<MyClass*>) only if it is really necessary. It will cause you some troubles with memory management, so let's choose an easier way.

Ok, let's talk about this question: Should this method also allocate memory for this vector?
If the purpose of this method is to create a vector, then yes, the method should also allocate memory for it:

vector<MyClass>* myMethod()
{
    vector<MyClass>* v = new vector<MyClass>;
    // initialize, fill or do whatever with this vector
    return v;
}

caller should clean up then:

    vector<MyClass>* v = myMethod();
    // do some stuff
    delete v; // clean up

If the purpose is just to retrieve the pointer to some specific vector that can not be obtained by caller, it could look like this:

vector<MyClass> _x; // caller can not access _x but myMethod can

vector<MyClass>* myMethod()
{
    return &_x;
}

caller shouldn't delete this vector in this case:

    vector<MyClass>* v = myMethod();
    // do some stuff, don't delete v

Upvotes: 8

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 71019

Yes, you may return a pointer to a vector and meaning of this is that you want to be able to modify this vector. Just keep in mind that you must not return a pointer to local variable.

As for pushing pointers in the vector - it depends on what do you want to do, but you don't HAVE TO push poiners.

Upvotes: 1

CB Bailey
CB Bailey

Reputation: 793349

A vector can be a vector of objects or a vector of pointers. This is completely independent of whether you have a vector or a pointer to the vector.

If you are starting out with C++ try to avoid pointers altogether. Just return a vector of objects. Remember that the vector contains its contents. When you put an object into a vector it will be copied in.

Upvotes: 10

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361812

In C++11, you can simply return std::vector<T>, and it will be fast. No temporary goes useless, as std::vector supports move-semantic, which means the resources allocated for temporary will be stolen by the move-semantic functions.

And if your type T manages resources, then you should implement move-semantic as well, along with usual copy-semantic.

Upvotes: 5

Related Questions