std::vector of references

后端 未结 3 788
后悔当初
后悔当初 2020-12-08 08:10

I have such problem: I have class Foo, and if have some objects of this class,

Foo a();

I need to put this object to 2 differ

3条回答
  •  無奈伤痛
    2020-12-08 08:27

    You need a vector of references. And since you specify that you need to use std::vector, then the correct thing to do is wrap your objects in the std::reference_wrapper. This page from C++ reference should explain it nicely:

    vector> vA, vB;
    vA.push_back(a);
    vB.push_back(a);    // puts 'a' into both vectors
    
    // make changes to 'a'
    // use .get() to get the referenced object in the elements of your vector
    // vA[0].get() == vB[0].get()
    

提交回复
热议问题