• Advertisement
Sign in to follow this  

Trouble accessing class Variable

This topic is 2206 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

I've been working on some 2D Movement in my games for the last few weeks and i'm using the SDL game libary for this game. I have a [font=courier new,courier,monospace]Player[font=arial,helvetica,sans-serif] class with a SDL_Rect embedded in it for tracking x,y coordinates. The problem is that my Player will not move because my [font=courier new,courier,monospace]Input()[font=arial,helvetica,sans-serif] method does not change the X Value. After doing some debugging, I figured out that my [font=courier new,courier,monospace]Input()[font=arial,helvetica,sans-serif]method[/font][/font] works and my [font=courier new,courier,monospace]Draw()[font=arial,helvetica,sans-serif]method also works, but the [font=courier new,courier,monospace]Input()[font=arial,helvetica,sans-serif]method does not change the values at all.[/font][/font][/font][/font][/font][/font][/font][/font]

[font=courier new,courier,monospace][font=arial,helvetica,sans-serif][font=courier new,courier,monospace][font=arial,helvetica,sans-serif][font=courier new,courier,monospace][font=arial,helvetica,sans-serif][font=courier new,courier,monospace][font=arial,helvetica,sans-serif][font=courier new,courier,monospace]Input():[/font][/font][/font][/font][/font][/font][/font][/font][/font]


void Input(SDL_Rect Rect)
{
SDL_Event Event;
while(SDL_PollEvent(&Event))
{

if(Event.type == SDL_KEYDOWN)
{
if(Key == SDLK_LEFT)
{
Rect.x--;

}

if(Key == SDLK_RIGHT)
{
Rect.x++;
}
}
if(Event.type == SDL_QUIT)
{
Exit();
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
C++ and C use "pass by value" by default. That is, when you call Input(), the rectangle in the calling function is copied into the "Rect" parameter of the function. Your function makes modifications to the local rectangle, but they are lost when this function returns.

One approach is to pass the rectangle by reference. In C++, you would write:

void Input(SDL_Rect &Rect)
{
// ...
}

The ampersand by the parameter asks the compiler to not make a copy, but to instead allow you to manipulate the caller's value. You don't have to change the code in the function, that remains the same. You do have to update any function declarations you have written for this function though to match.

C does not allow pass by reference, but you can simulate it by passing a pointer to a value. You must remember that this pointer itself is passed by value though. You can also do this in C++, but see below for why it might not be a good idea.

It would look like this:


void Input(SDL_Rect *Rect)
{
SDL_Event Event;
while(SDL_PollEvent(&Event))
{
if(Event.type == SDL_KEYDOWN)
{
if(Key == SDLK_LEFT)
{
Rect->x--;
}

if(Key == SDLK_RIGHT)
{
Rect->x++;
}
}

if(Event.type == SDL_QUIT)
{
Exit();
}
}
}

Note the arrow syntax required to dereference the pointer to access the member variable.

However, the C function does introduce an interesting new problem, what happens if the caller accidentally or intentionally passes a NULL pointer to the function? A simple solution is to document that the function does not allow NULL parameters. A better solution is to do this, but also use the assert() macro to validate that the parameter is not null in Debug mode. Finally, we could make the function "NULL safe", by not attempting to write to the Rect pointer if it is NULL.

For these reasons, I would prefer the C++ solution if you have a choice. It requires a not-null value by contract, so you don't have to worry yourself about working with the case where the pointer might be null.

Share this post


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

  • Advertisement