Computing.Net > Forums > Programming > c++ pointers with arrays

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

c++ pointers with arrays

Reply to Message Icon

Name: Martyn999
Date: November 9, 2006 at 03:11:20 Pacific
OS: Windows XP Pro SP1
CPU/Ram: Athlon64 3500+ / 1GB
Comment:

I have a base class Shape, then 3 more which inherit from this: Square, Triangle and Circle. I have an array of Triangle objects, and am working with virtual functions using polymorphism. If I make a pointer to the first element of the Triangle array, I can use ptr->Draw(); to display this. To get to the second element I can use ptr++;, then the draw method applies to the second element.

My problem is that I need to make the pointer of type Shape, then set it to point the the first element of the Triangle array. This first one works, but when I use ptr++; to move to the second element it becomes a pointer to some address which I can only imagine would be the next array of Shapes, so basically it doesn't advance the Triangle array. Is there any way to be able to loop through each element in Triangle apart from making a pointer array to each element?

Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: Guy
Date: November 9, 2006 at 17:27:36 Pacific
Reply:

I think you should post some code.

My guess is that you are declaring an array of Triangles and should be declaring an array of Shapes.

Maybe some casting involved after that.

Guy


0

Response Number 2
Name: Martyn999
Date: November 10, 2006 at 05:36:56 Pacific
Reply:

I've done it a different way now, I simply call the function from a loop, passing it the next element each time.

Thanks anyway.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: c++ pointers with arrays

Exit for loop with arrays in C?! www.computing.net/answers/programming/exit-for-loop-with-arrays-in-c/8687.html

C 2-D array www.computing.net/answers/programming/c-2d-array/8058.html

Pointer to array of character pointers? www.computing.net/answers/programming/pointer-to-array-of-character-pointers/2809.html