references

Started by
1 comment, last by SiCrane 18 years, 4 months ago
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?
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.

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.

This topic is closed to new replies.

Advertisement