Passing vectors by reference

前端 未结 3 645
野性不改
野性不改 2021-02-01 05:24

If I have a vector of objects in one class which I want to change in another, I would try and pass all the information by reference.

What exactly do I need to pass by re

3条回答
  •  被撕碎了的回忆
    2021-02-01 05:38

    You cannot have a vector of references. Vector elements must be copyable and assignable, which references are not. So only the first option is actually an option, but it's spelled std::vector &.

    Note that v[1] already returns a reference to the second element, so you can happily pass individual elements by reference.

    It is possible to have a vector of reference-wrappers a la std::ref, but if you don't know what that is, you probably shouldn't be using it at this point.

    提交回复
    热议问题