Vector of References: Why the Prohibition?
When attempting to declare a vector of references, as in std::vector
References are not assignable. Once a reference is initialized to an object, it must always refer to that object and cannot be modified to point to a different object. This violates the assignability requirement for containers like vectors.
Unfortunately, this means that storing references to structs in a vector is not directly possible using the std::vector
It's important to note that working with pointers requires careful memory management. You must ensure that pointers are properly allocated and deallocated to avoid memory leaks or dangling pointers. If you are unfamiliar with pointers, it's recommended to use a more beginner-friendly data structure for managing references to structs, such as std::list
The above is the detailed content of Why Can't I Create a Vector of References in C ?. For more information, please follow other related articles on the PHP Chinese website!