Sign in to follow this  

Classes

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

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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4243 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this