Question: I'm a Java developer and I'm new to C++.  

With Java: I can do the following

```
myArrayList.add(obj);

```

in 'myArrayList' I will find a reference (a pointer) to 'obj'  

However with C++: I know that

```
std::vector::push_back(obj);

```

Will insert a copy of 'obj' into the vector and not a reference. This would usually not be a problem if it wasn't for the high number of operations and objects created. 

*My problem:*  

Performance problems due to high number of created and duplicated objects by using std::vector.  

*What I need:*  

an alternative to std::vector or some other way to make dynamic lists of references (pointers) without duplicating objects.

PS: I know about making an array of pointers like this

```
MyClass** dynamic_array = new MyClass*[max_size];

```

I can't use this as the *max\_size* is never known. 

Thanks for the help.

Answer: 
You can store a reference wrapper instead:

```
#include <functional>    

MyClass item;
::std::vector<::std::reference_wrapper<MyClass>> items;
items.emplace_back(item);
// accessing an item will involve an extra call of get() method
items.back().get();

```

Note that (unlike in Java) objects are not managed by GC, it is programmer's responsibility to ensure that references object survives long enough so stored reference is valid.
An easy way to solve this is to just use pointers. Here is an example:

```
struct N { int x; };

void test()
{
    N temp;
    std::vector<N*> x;
    x.push_back(&temp);
}

```