oop question

Started by
20 comments, last by phil67rpg 10 years, 5 months ago

when do you use the public and private data members.

Advertisement
private data-members are used if you want hide those data members from other classes or the user himself.
public members are used if you "don't care" what happens to your member.
easy said...
but it's possible to explain it more specific

My rule of thumb is this:

I use classes whenever I have logic to manipulate my data members. Then all the data members are private, and I have setters/getters for them. For example, CVertexShader() - it has logic for creating, destroying, setting a vertex shader, and I don't expose any of it's internals. This allows better abstraction, and if I ever need to change something with vertex shaders, it keeps the changes localized.

For data containers I use structs and makes everything public. Basically, stuff that I don't need to manipulate, just store and retrieve data. In that case setters/getters are just a hassle. If at any point this data starts to have logic - I convert it into a class.

Putting it all together - my constants buffer objects are classes with private data members, but the CBs data containers are structs. This allows my UI thread to easily change the CBs data, while hiding all the API logic inside a class.

Formally, each class has an implicit property called class invariant. It's the assumption you can always make about each object instantiated using that class.

In practice, we must ensure each object is valid - invariant is satisfied - and you can guarantee this only by taking each modification attempt and verify it transitions your object to another valid state.

Of course, you cannot do that on stuff that is directly assignable. So, as above, public members are "don't care".

In line of concept, you should use private on all other cases. But in reality protected is often a good compromise, due to specificity of derived class code - if someone is writing a derived class, he has to deal with much more focused context than the whole program. Someone think protected is bad and to a certain degree I agree with them but in the real world, I find it very convenient.

Edit: derided class code... what a typo!

Previously "Krohm"

Theoretically everything outside a class shouldn't need to know about its data, thats why they are private by default. Using public is like exposing the innards of the class to the whole world.

If following "tell, don't ask" principle you would also just call a method to tell an object what to do; not use a getter to inspect it, do some outside decision and use a setter to change it from outside.

You want to make variables in classes private, ALL the time. But what you want to do is make an Accessor Function, or a "getter". This will allow you to obtain the variable even if it is private. But make sure you make the functions public, so you will be able to access them. You can also use a setter function, this will allow you to set the value of the private variable, but it is a public function, so you will be able to access it.

~GTE

I don't agree.

If there are no invariants to worry about, and the implementation is simple and obvious (i.e setting a variable and doing no verification of validity), making something public is fine.

I wouldn't have setX, getX, etc. for a 3D vector class, since all values are valid.

If there was any reason to limit the bounds of a 3D vector (e.g. physics constraints on world size), then the implementation is no longer simple and obvious so then get/set methods would be appropriate.

All pointer members should be private though.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley


I wouldn't have setX, getX, etc. for a 3D vector class, since all values are valid.

Not to be nitpicky here, but what about values like NaN or infinity? NaN could definitely break the conditions for satisfying the class invariant for a vector class

It's all about design of course; I don't use mutators and accessors in my vector classes either since you should be able to avoid cases like NaN quite easily by just using common sense

I gets all your texture budgets!

That's handled by the floating point specification, and adding checks is going to affect performance.

You could put debug only checks in, then you would use an accessor and inline it. I wouldn't do that for a vector class though. I have used it for things like verifying arguments are within expected ranges e.g. for inverse trig functions.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

You want to make variables in classes private, ALL the time. But what you want to do is make an Accessor Function, or a "getter". This will allow you to obtain the variable even if it is private. But make sure you make the functions public, so you will be able to access them. You can also use a setter function, this will allow you to set the value of the private variable, but it is a public function, so you will be able to access it.

Don't just blindly make every class member private with an accessor; sometimes there should be no accessor, and sometimes public access makes more sense. The sensible thing to do is actually consider the invariants and intended usage of your class and make members private and/or provide accessors when it's genuinely the most appropriate design.

Plain setters are sometimes valuable, but not always the best idea; it often makes more sense to provide a semantically relevant function that might adjust multiple values rather than a simple setter - for example, rather than providing setters for x and y values you might provide a single move() function that sets (and perhaps validates) both values.

- Jason Astle-Adams

This topic is closed to new replies.

Advertisement