is there any reason why
foo = (bar->at(x))->at(y);
works but
foo = bar[x][y];
does not work, where bar is a vector of vectors (using the c++ stl)
the declaration is:
std::vector< std::vector < Object * > * >
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Is it a vector of vectors or a vector of pointers to vectors? Your code should work as advertised:
But your code appears to have:
If you have pointers, you’ll need to dereference them first to use
operator[]:That that can be ugly. Maybe use references temporarily:
Though that still has some ugly. Maybe free-functions are helpful:
Most preferable is to be rid of the pointers, though. Pointers can leak and have all sorts of problems, like leaking when exceptions are thrown. If you must use pointers, use a pointer container from boost for the inner vector, and store the actual object in a smart pointer.
Also, your title is a bit misleading. The difference between
atandoperator[]is thatatdoes range checks. Otherwise, they are the same.