I have just begun working with pointers in C++ and I am having trouble creating an array of pointers to Objects. Is there any standard that can be followed when creating them? What would the coding look like? Any help would be really hot! Thank you
Well you would get better help in the C++ forum, as this is mainly for Visual C++ (but then again...)
ok an array of pointers, hmm, well your pointers are needing to point to data you want to hold, and certainly you cant make a pointer point at two things at once.
show you a linked list, perhaps that might help.
struct node
{
int somenumber;
node *next;
}head_ptr, current_ptr;
current_ptr = current_ptr->next;
current_ptr->next = NULL; // to set the next of the second guy to null
after a while if you mananged to create alot, lets say you'd like to find the number 10.
if (Head_ptr != NULL) //got to make sure the root pointer is not pointing to a null
{
current_ptr = head_ptr //sets current to the beginning
while (current_ptr->next != NULL)
{
if (current_ptr->somenumber == 10)
cout<<"This one is the same";
current_ptr = current_ptr->next;
}
}
that'll just loop throught the entire linked list until it's next is = null, of course the problem there is that if you only have one item, and it's next is null, well you pretty much skip that guy which is why its better to do
while (current_ptr != NULL)
....
so that as soon as it does hit the end, it doesnt try to check the null value.
I am not certain about "Arrays of Pointers" but to me, a linked list is closest I can get to explaining it.
Karl
kb244@kb244.com
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.