# passing a vector of unique_ptr to function

This topic is 379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone....

I have :

std::vector<std::unique_ptr<Base>> v;

How should v be passed to a function.  Could you please show me the declaration and the function call?

Thank you I need it for polymorphism,

Josheir

##### Share on other sites
void function(const std::vector<std::unique_ptr<Base>> &v) {
// ...
}

void calling() {
std::vector<std::unique_ptr<Base>> v;
function(v);
}

##### Share on other sites

This might be a silly response, but what are you trying to achieve?

Unique_ptr implies single ownership of a pointer without the ability to copy it.

When you pass an array into a function by value, you are making a copy of it.

If that was your intent, then you cannot copy the individual unique_ptrs in the array, and as such cannot copy the array itself. If that was your intent, you need to use shared_ptr or weak_ptr instead.

However, if you just wanted a reference to the array inside the function then you should pass the array by reference. This is good when you only want:

• A read only copy of the array
• You are interested in any changes that you make inside the function to propagate out of it

In this case, you should declare the function definition to accept a pointer (*), or a reference (&) to the array

##### Share on other sites

I wanted to use polymorphism so that the correct derived class function can be chosen by a function and use it's data.

I wanted a smart pointer instead.  I also wanted to have one static sprite/texture instead of every object having an image.  The derived classes will hold a static image for all the objects of that vector.

Some functions will accept a vector and loop through it for various purposes like comparing positions of objects.

I want to be able to pass to the function the base class and than sometimes use polymorphism (pure virtuality I'm thinking) to display a different image in a virtual function that are "owned?" by the different derived classes.

The question is for tomorrow and I am unhappy that I need to change all of the code from the previous version from . to -> but I want to use polymophism with different derived classes, basically.

Sponji, I was thinking to myself there after I posted and I can't believe I got it right too!  Thank you so much for the post.  it's for tomorrow.  It's very enlightening, that post for me.

Thank you as well, SillyCow!

Thanks again,

Josheir

So I want a smart pointer and I am  " interested in any changes that you make inside the function to propagate out of it "  so I will use a reference to the "array" or vector.

Edited by Josheir

##### Share on other sites
However, if you just wanted a reference to the array inside the function then you should pass the array by reference. This is good when you only want: A read only copy of the array You are interested in any changes that you make inside the function to propagate out of it In this case, you should declare the function definition to accept a pointer (*), or a reference (&) to the array

I am assuming that by propagate you mean that the changes to v will change the vector outside of the function too.  And therefore if I want a read only copy of array I pick * and if I want this propagation I pick & ?

I'm confused by "if you just wanted a reference..."

You are using this term reference in a casual manner (not an actual reference - but a regularly used word?)

Thank you,

Josheir

I have a lot of changes so I need to be certain this is going to work.

Edited by Josheir

##### Share on other sites

No, a pointer and a reference are basically the same thing, just different syntax. Both say that the object inside the function is exactly the one that was passed in, which is why both are essentially passing the vector 'by reference'.

If you are just reading the vector inside the function, then passing it in as a const reference is fine. Sponji's example is what you'd want.

If you're doing something more complex, you might need a different pointer type. It's hard to say exactly, given your abstract initial question and quite wide-ranging concrete follow-up.

##### Share on other sites

Let me see if I can narrow down this range than to get helped.

I started with this:

std::vector<asteroid>largeAsteroid;

I pushed onto the vector like this:

push_back(largeAsteroid());

Than I passed different vectors to  functions like this:

function(int numofasteroids, std::vector<asteroid> & v)

than in functions I used the vector with indexes to call member functions like get and set like this:

...v[j].getY()...

Everything works great except every object now has an SFML sprite with texture and there are more than one textures.

So...I wanted to use polymorphism with two more classes that will derive from asteroid.

As I understand it the vector must than hold pointers and my std::vector<asteroid>largeAsteroid; will not work.

So now, I guess it's :

vector<shared_ptr<asteroid>> or  vector<asteroid*>

And I imagine I should use a smart pointer if I can.

Thank you,

Josheir

##### Share on other sites

Yes, if you have multiple subclasses of Asteroid, you can store them in a vector like so:

std::vector<std::shared_ptr<Asteroid>> all_my_asteroids;

But if the only thing that differs is the texture, you don't need different classes. You just need to change the texture member in the relevant asteroids. (Similarly, if all that differs is the texture and the size, just change both members.)

So you may not even need polymorphism here. Usually you need polymorphism when you want 2 related types to behave slightly differently when the same function is called, and where that difference can't be achieved easily with simple data differences.

##### Share on other sites
No, a pointer and a reference are basically the same thing, just different syntax. Both say that the object inside the function is exactly the one that was passed in, which is why both are essentially passing the vector 'by reference'.

AND:

//////////////

But if the only thing that differs is the texture, you don't need different classes. You just need to change the texture member in the relevant asteroids. (Similarly, if all that differs is the texture and the size, just change both members.)

//////////////

Made my day, very enlightening!  Syntactic sugar for example, so great.

Thank you Kylotan,

Josheir

Edited by Josheir

• 32
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631352
• Total Posts
2999483
×