Re: need help creating a two dimensional vector that holds pointers
of a user defined type
Thanks for the quick reply. Even though I've technically been
programming in C++ for eight years, I've always been really really bad
at it, so it's nice to be able to talk with people that really know
their stuff. You were correct about the assert and the vec.push_back
(new ABC(i));
On Apr 7, 11:13 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
So, it's a non-static data member, right?
Yes.
...and in my destructor I have:
for (std::vector<ABC*>::iterator it = vec.begin();
it != vec.end();
it++)
{
delete *it; *it = 0;
I can understand deleting (since you allocated it using 'new'), but why
do you care to set it to 0? The vector is going to be destroyed right
after the destructor's body finishes...
I never really understood the rule about setting pointers to zero, it
was always something my professor told me to do "to be on the safe
side." So I just ended up setting all my pointers to zero after delete
statements without really giving much thought as to what it did.
So, when do need to set the opointer equal to zero and when is it
unnecessary?
It's a 'vector<ABC*>', of course! So, you need to push an instance of
that object onto your 'vec' before entering the inner loop:
for (int i = 0; i < 7; ++i)
{
vec.push_back(vector<ABC*>()); // now you have i-th eleme=
nt
for (int j = 0; j < 5; ++j)
vec[i].push_back(new ABC(j));
}
OK, now here is something I am totally clueless about:
vector<ABC*>()
What is with the two parentheses at the end? Does that mean that it's
an empty vector? Is there anything we could put in those parentheses
if we wanted to?