Original Post
🔒 Locked
[Pointer to a vector of pointers] push_back ()
Advertisement
Your compiler should really give you a warning for this kind of thing. For instance, this is a C4700 for MSVC, which is a level 1 warning. If you warnings enabled at all, MSVC will complain that you're using an uninitialized variable. Compilers emit warnings for a reason. Pay attention to them. If your compiler didn't give you a warning for this, then it's time to get a new compiler.
Quote:
Original post by nGamer
Ah, I see. Thanks, guys.
If you're going to use it beyond the current scope, do this:
std::vector<Object *> *vector_pointer = new std::vector<Object *>vector_pointer->push_back(new Object);delete vector_pointer;The local variable (first line in my first reply) will be destroyed at the end of the scope (that is, when a closing bracket is met '}'). If you want to use the pointer also beyond the scope, you must use dynamic allocation as above. Don't forget to free/delete the memory at the end of the program / when you don't need it anymore!
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement