• Advertisement
Sign in to follow this  

references

This topic is 4436 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to use lists of references instead of lists of pointers, for the same purpose (for example pointing to different objects and child objects from an array in such a way that virtual functions are called the same way as with pointers). And is it possible to make a reference to an element of an std::vector. If so what happens if that element disappears from the std::vector?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Lode
Is it possible to use lists of references instead of lists of pointers, for the same purpose (for example pointing to different objects and child objects from an array in such a way that virtual functions are called the same way as with pointers).


It is not possible with STL containers, because references cannot be reseated. However, you could implement your own list class which could use references to stored objects instead of pointers. I'm not sure what the purpose of this would be, however.

Quote:
And is it possible to make a reference to an element of an std::vector. If so what happens if that element disappears from the std::vector?


Yes, it is possible. References, pointers and iterators to elements of a vector are (potentially) invalidated by operations of deletion, insertion or swapping.

Share this post


Link to post
Share on other sites
If you mean a std::list, then, no. You can't create a standard container instantiated with a reference type.

You can create a reference to an element of a std::vector; that's the type that operator[] returns from the vector. References to vector elements are invalidated in the same way that pointers and iterators to vector elements are.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement