Question

I started a small game in C++ using SDL. I have been looking into pointers and references and I understand the differences.

int main(int argc, char* argv[])
{
    SDL_Renderer* renderer = NULL;
    TTF_Font* font = NULL;

    renderer = SDL_CreateRenderer(window, -1, 0);
    if(renderer == NULL)
    {
        return 1;
    }

    font = TTF_OpenFont("OpenSans.ttf", 12);
    if(font == NULL)
    {
        return 1;
    }

    texture = renderText(...)

    return 0;
}

SDL_Texture* renderText(...)
{
}

This is part of my program, now I need to pass the renderer and font objects to the renderText() function. My question is, should I pass them as pointers (which they already are), or as reference?

This is how I think it would look:

SDL_Texture* renderText(SDL_Renderer* renderer,...)
{
}

renderText(renderer,...)

or

SDL_Texture* renderText(SDL_Renderer &renderer,...)
{
}

renderText(*renderer,...)

Also later in renderText() func I have to pass pointer of the SDL_Renderer into other SDL functions so I would have to use & again in the second case.

Was it helpful?

Solution

As long as you are dealing with SDL objects, you should keep them as pointers, because you are just going to have to turn them back into pointers when you pass them back to the SDL functions. You should, for the most part, think of pointers to SDL resources as opaque.

OTHER TIPS

SDL is a C library. Both ways that you suggested will work, but C code has no references so you will end up using & operator every single time you want to pass an SDL_ structure to an function. So its better to use pointers in your example, but even better way would be creating a small SDL C++ wrapper.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top