• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 656
  • Last Modified:

Vector of references?

Can I use a vector (or any container) of references? Or must I use pointers?

If not, why not?

std::vector<Object&> vector_of_references;
  • 2
1 Solution
>> Can I use a vector (or any container) of references?


>> Or must I use pointers?

That is one option, yes.

>> If not, why not?

References always need to refer to an existing object. You cannot declare a reference without specifying which object it refers to. So, you cannot have a container of references.
May I ask why you gave a B grade ? That usually means that something was missing in the answer and/or that something is still unclear. If that's the case, then do not hesitate to ask for clarification where needed.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now