• Advertisement
Sign in to follow this  

Return Pointers?

This topic is 4576 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Erm, sound newb and i guess it is ^^ Ok i have a class named CTiles. In my CGameMap I will make a 2D object array of CTiles. So every case of my array will be for a single tile. When i will want to draw the map, i will need the SDL_Surface so i can draw. So i make a function that return the SDL_Surface. But It's a pointer, so how do i return it? how is the prototype supose to look? Right now it look like that.

SDL_Surface*	GetTile(); //Return the SDL_Surface. For the draw function.




Is that right?

Share this post


Link to post
Share on other sites
Advertisement
I don't know anything about SDL, but if all you're doing is returning a pointer, then, yeah, that's the way to do it, provided that you're doing something like this:


SDL_Surface* surface_pointer = GetTile();
// or
SDL_Surface* surface_array[50];
surface_array[34] = GetTile();



Or something like that.

Share this post


Link to post
Share on other sites
Returning pointer types is perfectly valid, but there are considerations. If the pointer is to memory that was allocated within the function, where is the memory deallocated? Transferring that responsibility to the calling code is bad, and a violation of Resource Acquisition Is Initialization (RIAA), a very useful idiom for programming in C++ with minimal headaches.

Assuming you're working in C++, here's what I'd recommend: Encapsulate the SDL_Surface in an object that releases it when going out of scope, like boost::shared_ptr, and have your function return that.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Oluseyi
Returning pointer types is perfectly valid, but there are considerations. If the pointer is to memory that was allocated within the function, where is the memory deallocated? Transferring that responsibility to the calling code is bad, and a violation of Resource Acquisition Is Initialization (RIAA), a very useful idiom for programming in C++ with minimal headaches.
SDL_Surfaces are allocated and free'd by SDL. No violation here. Try again.

Have a nice day.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
SDL_Surfaces are allocated and free'd by SDL. No violation here. Try again.

That seemed unnecessarily brusque. Why the negativity?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Sneftel
Quote:
Original post by Anonymous Poster
SDL_Surfaces are allocated and free'd by SDL. No violation here. Try again.

That seemed unnecessarily brusque. Why the negativity?


No negativity intended.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement