Return Pointers?

Started by
6 comments, last by Sneftel 18 years, 6 months ago
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?
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();// orSDL_Surface* surface_array[50];surface_array[34] = GetTile();


Or something like that.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Alright Thank. Wans't sure about it
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.
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.
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?
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.
Okeydoke. My mistake.

This topic is closed to new replies.

Advertisement