Jump to content
  • Advertisement
Sign in to follow this  
AlbertoT

why private?

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

Hello Data are often declared as private in OOP. I wonder wether it is, in some cases, a useless precaution. I mean , If I write my own game and nobody else have access to my code, why should I protect my own data ? To prevent the data from assuming unwished values , it should be better to write , when necessary, something like if (x >= 100 ) x = 100; rather than a lot of access functions Thanks in advance for your support

Share this post


Link to post
Share on other sites
Advertisement
It isn't terribly important, but it is helpful to develop the habit of having member data only altered by the object it is contained in. I personally don't make most of my data private for pretty much the same reason.

Share this post


Link to post
Share on other sites
Quote:
Original post by AlbertoT
Hello

Data are often declared as private in OOP.
I wonder wether it is, in some cases, a useless precaution.
I mean , If I write my own game and nobody else have access to my code, why should I protect my own data ?
To prevent the data from assuming unwished values , it should be better to write , when necessary, something like if (x >= 100 ) x = 100; rather than a lot of access functions
Thanks in advance for your support





its part of OOP.
often you can only access private members when the object provides you with a function. even if it has just one line of code. sure when you write your own code and no one else is working with you its no problem to give a **** about private members.
but imagine a group of developers working on several parts of a project at one time. with the right class design(which also includes private class members) you can prevent many errors which would be hard to find when they occur later in the project.

greets deviceLost

Share this post


Link to post
Share on other sites
the concept of oop is
1. let the objects alter themselfs object->move(direction) instead of moveobject(object)

2. class members are private by default and you can add functions to access data
now imagine your object is in a certain state and the information you could access directly because you declared it public is invalid you had to check whether this information is valid or not where ever you access it
this a by far more work than a function that returns the information you need

small helper classes that just serve as containers(you could replace them with structs) of course don t need private members, but you should really be strict and make everything private except when you gain a advantage e.g.: increased performance

now in gameprogramming you could for example create a entity class and add a member
Vector origin;

make this private and accessing it by a memberfunction would be overkill because you access it serveral hundret times each frame

3. its a matter of code organisation and layout so don t make things public if you don t need it public


just a node

you certainly have heard about alignment

you can mix the public: protected: and private: statements in order to get perfect memory alignment

Share this post


Link to post
Share on other sites
When other people (or yourself, after a period of time) use your classes, it will be easier to understand how the class works. For example, one cannot change vital information that the class uses.

Share this post


Link to post
Share on other sites
Quote:
Original post by Pipo DeClown
When other people (or yourself, after a period of time) use your classes, it will be easier to understand how the class works. For example, one cannot change vital information that the class uses.
One cannot easily change said information. [wink]

Share this post


Link to post
Share on other sites
My rule of thumb is:

Use private scope variables when it is necessary to change other values too, like if you alter a transformation matrix and let some update mechanism know that something has changed (by means of bool vars). Use public scoped variables otherwise.

cheers

Share this post


Link to post
Share on other sites
Quote:
Original post by AlbertoT
To prevent the data from assuming unwished values , it should be better to write , when necessary, something like if (x >= 100 ) x = 100; rather than a lot of access functions


So why clutter up your code with checks like this ? Better put that check into the access function. This also allows to easily change the 100 to 50, in case thats needed.

In C++ you can inline those accessor functions, so you do not even loose performance, but gain flexibility, i.e. you may alter your data structures without the need to change the rest of your program.

Share this post


Link to post
Share on other sites
I love seeing code like this:

class SomeClass
{
private:
int data;

public:
void SetData( int data )
{
this->data = data;
}

int GetData( )
{
return data;
}
}



I think a lot of books overstress data-hiding (causing people to write code like the above). If I'm writing code I will use a lot, I bother with privatizing things - but not for a one-time thing.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!