Archived

This topic is now archived and is closed to further replies.

Private Parts

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

And now *drumroll begins* another stupid question from the mind of a newbie. I was kind of wondering what the advantages of private members are and if using private members slows your programs down or not. Are members declared private so that people will never access them outside of the objects they belong to? If that''s true, why do programming books always make methods used to access private members? example: int GetAge() {return age;} Also, I''d imagine that functions used to access private members just slow things down. If I''ve got a member that I need to access from outside the object, should I declare it as a public member? -Forcas
"Elvis is alive. He is Barney the purple dinosaur. He is the pied piper that leads our children into the wages of sin and eternal damnation."

Share this post


Link to post
Share on other sites
Here is a short, condensed, and incomplete answer; but it should give you some idea why they are used.

It helps insure that the class is used as the programmer intended. For example, by only providing a public Get***() member function, data can be made read only. Or if changing a variable means another needs to be recalculated that would be a good reason to to put that in a Set***() member. It also provides a single place to change should conditions about a variables use be changed.


Jack

Share this post


Link to post
Share on other sites
Objects that you create should be black-boxes, think of a VCR. There are a bunch of buttons on the front that let you control what the VCR is doing, but the internals are hidden from you. What''s nice about this is that all you as the user of the VCR needs to know is that if you put a tape in and press the play button, the tape will play. You don''t need to know what''s happening behind the scenes (pulling the tape into position, aligning the head, decoding the data into video signals, etc.). No matter what VCR you''re using, or who made it, you can always just push the play button and watch your movie.
By making your members private and forcing users to use an accessor method ( getWhatever(); ), you can change the way that the value is retrieved or calculated without changing any of the code that uses that class. And as JackNathan said, you can create mutator methods ( setWhatever(var whatever); ) that let you maintain consistancy inside the object. Yes, it is slower to call a method than to access a member variable directly in most languages; here is where you need to consider the ways the class will be used. If this variable will be accessed 500 times a second inside the render loop, you might want to bite the bullet and just make the member public. However, if the member is accessed infrequently and in non-time-critical places, take 30 seconds and write accessor/mutator functions, you''ll be glad you did later.


"If consquences dictate our course of action, it doesn''t matter what''s right, it''s only wrong if you get caught."
- Tool

Share this post


Link to post
Share on other sites
The whole get/set methodology was the hardest thing for me to justify when using classes, and I still rarely use it.

The intent however, is as explained...black box and all that, but it''s also simply for data validation...As far as the Set function is concerned, but once again, I rarely use it : )

G''luck,
-Alamar

Share this post


Link to post
Share on other sites