Can I store items in a vector by reference?

I've got a vector of Transforms:

1
2
3
4
5
6
  Transform position_a;
  Transform position_b;
 
  std::vector<Transform> transforms;
  transforms.push_back(position_a);
  transforms.push_back(position_b);


I'm testing if a ray shooting from my mouse position, intersects with any of the transforms stored in a vector. But the problem is, if one of the transforms changes positions after it was stored in the vector, the positions in the vector remain the same:

1
2
3
4
5
6
7
8
9
10
11
  Transform position_a;
  Transform position_b;
    
  std::vector<Transform> transforms;
  transforms.push_back(position_a);
  transforms.push_back(position_b);
  
  position_b.GetPos()->x = 6.0f; // Change Position

  ShootRay(); // for loop iterates through vector of transforms and test intersection


The ray thinks position_b is still in the same position when I call the ShootRay function.

Is there a way I can store items (in my case Transforms) in vectors by reference? That way I can change the position on the original variable and it will automatically change the referenced object in the vector?

Or do I have to to modify the item in the vector and just work with the object in the vector from there on? I'm a little confused. Thanks and hope I didn't make this confusing.
closed account (E0p9LyTq)
No, not directly as references.

http://stackoverflow.com/questions/922360/why-cant-i-make-a-vector-of-references
Last edited on
You are, however, free to store pointers to your objects in a vector.

-Albatross
closed account (E0p9LyTq)
You are, however, free to store pointers to your objects in a vector.


http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
Thanks guys, this helped!
Topic archived. No new replies allowed.