Skip to main content
gamedev.net
🔒 Locked

Classes

Started by gamechampionx Oct 10, 2002 at 2:17 PM 3 replies 750+ views
Original Post
gamechampionx
gamechampionx
I''m new to classes, and I have a quick question. When would you not make a class variable public? What''s the point if you have to access it from another function? Also, if I want to use a class variable, but have its scope only in one function, would I simply declare that instance of it in that function, or do something with making it private, and using pointers or something?
Arild Fines
Arild Fines
quote:
Original post by gamechampionx
When would you not make a class variable public?


Always.
quote:

What''s the point if you have to access it from another function?


You never have to.
quote:

Also, if I want to use a class variable, but have its scope only in one function, would I simply declare that instance of it in that function, or do something with making it private, and using pointers or something?

Rephrase.



The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
AdamMB
AdamMB
quote:
Original post by gamechampionx
When would you not make a class variable public? What''s the point if you have to access it from another function?



You probably shouldn''t ever make a class member variable public. (Or at least try not to do it if there is any way around it.) The idea with classes is that you should make it''s variables private (or protected depending..) and then provide "Accessor" methods to actually manipulate the variables. The reasoning behind this is that you can have more control over what goes into the variables. e.g. you have a class that stores fractions called cFraction. you create the object:

cFraction fraction;

Then you try to set the num and denom:

fraction.SetNum(10);
fraction.SetDenom(0);

Since I''m using Set/Get methods to access the variables, I can see that the 0 I am trying to set the denom to is illegal and can handle it myself in whatever way I like. Had I just had 2 int variables, I could set them to anything I wanted and I wouldn''t catch this type of error until the Div by Zero exception occured. (And yes, I know you would probably set the num/denom in the constructor instead of like this, but this is they first example that came to me...)

Secondly, classes are all about information hiding (encapsulation) and these Set/Get methods allow you to change the internal variables without breaking the code that is using your class. e.g. If the cFraction class had an int called num and I didn''t use Set/Gets, then I would access it like:

fraction.num = 10;

That works fine until I (for whatever reason) figure out I needed to change num to m_num. So now I have to go to every line in the program using the class and change num to m_num. But if you use Set/Get methods, you simply change that line and the end user of the class (if it wasn''t you) wouldn''t know the difference (and wouldn''t need to know). Keep in mind that this whole thing is a silly example, but I hope you can start to see the point at least.

-Adam
gamechampionx
gamechampionx
OK, I have my current game code posted at this place.

If you read it, you can see that I use a struct to keep character info stored for use in many different functions. I now understand that I can make the stats private, but make public functions that can change them. The question is, is it right to still declare the player's object outside a function so that it can be accessed from anywhere? Or if I include in in main, will it do the same (I think no). What should I do about this.

Also, how should I do item stats? The equipped item will probably be transfered to a stat in the character class, but if I make it private, should I declare the items as public objects (outside a function), or reinitialize them each time you battle?

Lastly, should I use void or int for void-type functions? Is void any faster / space consuming?

Thanks for all your help!

[edited by - gamechampionx on October 10, 2002 7:06:14 PM]
Beer Hunter
Beer Hunter
quote:
Original post by Arild Fines
quote:
Original post by gamechampionx
When would you not make a class variable public?
Always.

std::pair?

Better to say: When there will never need to be any restrictions on who can change the data or what values can be stored, and it''s less obfuscated than the alternative (which is very rare indeed).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.