Classes

Started by
3 comments, last by Silent Dragon 17 years, 11 months ago
Hmm, I was just wondering how the best way about making a class would be. The class I have created for a sprite for my 2nd Pong clone is as follows..

class Sprite
{
      public:
             Sprite(char *Filename);
             ~Sprite() { SDL_FreeSurface(sprite); }
             void Draw(SDL_Surface *dest);
             void UpdatePos(int new_x = -1, int new_y = -1) { if(new_x != -1) x = new_x; if(new_y != -1) y = new_y; }
             int GetH() { return h; }
             int GetW() { return w; }
             int GetY() { return y; }
      
      private:       
             SDL_Surface* sprite;
             
             int x;
             int y;
             int w;
             int h;
};

I don't see much of a benefit of putting the variables as private because to set and get variables from there requires a function to get and a function to set? Is there any easier way to do it? Or any suggestions as to an alternative to a class is a class isn't the best choice? Thanks for your time, SD
Advertisement
Quote:Original post by Silent Dragon
I don't see much of a benefit of putting the variables as private because to set and get variables from there requires a function to get and a function to set? Is there any easier way to do it? Or any suggestions as to an alternative to a class is a class isn't the best choice?

The benefit is that you can change the way those values are calculated without the client (outside the class) knowing about it. The more public members you have, the more outside code may be dependent upon them, which makes design changes arbitrarily more of a pain in the butt, since any change in the implementation requires a change in all the code that uses it. Data should be private; expose an interface to access your data (get and set).
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Quote:Original post by stylin
Quote:Original post by Silent Dragon
I don't see much of a benefit of putting the variables as private because to set and get variables from there requires a function to get and a function to set?

The benefit is that you can change the way those values are calculated without the client (outside the class) knowing about it. The more public members you have, the more outside code may be dependent upon them...


However, it is unlikely that the behavior of those values will ever change, so in this case you could consider accessors as unnecessary complexity. Your point about too many public members is valid, but using accessors is not a solution to that problem.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
yeah, the benefit of using get and set methods, rather than accessing the variable directly is that you have control over the data that is 'set', for example, you can check that it is within a certain boundary or something in your get method. If you just want anything or anyone to be able to access this data, then potentially the value could be changed to anything, and you would have no control over it. If you are using a function to do this, you can make sure that only valid data is entered.

Unless you're changing this class member constantly, the overhead from excess function calls is most likely negligeable to the speed of your program anyways, and I'd say it will make your code a little safer anyways
Thanks for your help. I'll leave it with Get and Set functions :p

This topic is closed to new replies.

Advertisement