Proper C++ Programming

Started by
14 comments, last by LilBudyWizer 19 years, 7 months ago
Quote:Original post by MonkeyCookie
For collision detection, you'll want to know the positions of at least two objects, the code that does the collision detection will have to get the position of at least one of the objects, no matter where the code is located.

But do you really need accesors for that?
Depending on the architecture, the object might just as well notify the collision sub-system about world position changes.
Advertisement
Quote:Original post by Dmytry
it's not an issue with Delphi, where you have "properties" that can be mapped to variables or to function, and you can swich to using properties at any stage of work, when you'll think it's needed.
)


All modern C++ compilers have a property extention - C++ Builder works the same way Delphi does!
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Quote:Original post by darookie
Quote:Original post by MonkeyCookie
For collision detection, you'll want to know the positions of at least two objects, the code that does the collision detection will have to get the position of at least one of the objects, no matter where the code is located.

But do you really need accesors for that?
Depending on the architecture, the object might just as well notify the collision sub-system about world position changes.


This garuantees a duplication of data which almost always horrendous bad and sometimes just really bad.

The OO belongs at a higher-level. Use it hook the objects to the services they need, then let the data flow uninhibited. With C++ you don't have to use OO and virtual methods to decouple the components.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Quote:Original post by Magmai Kai Holmlor
All modern C++ compilers have a property extention - C++ Builder works the same way Delphi does!

Last I heard GCC refused to implement it, which is a huge shame. Of course, they support typeof() just fine. I think one of the arguments leveled against properties was the fact that you can contort templates into doing it for you...which is impressive but you end up having to store member function pointers all over the place. :(
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
The reason why we encapsulate data meaning making data members private to enforce access and update through member functions i.e. getters & setters are:

1. You determine that your type's represenstation is variable, meaning its data members can change over-time be it the name or the type of the memeber, if you did not encapsulate the variant then clients of your type will be tightly coupled to its representation thus if your type's represenstation changes then all clients are forced to update there code to reflect the changes and then recompile it all.

By encapsulating the variant you make loose coupling by indirection, enforcing clients to access/update through an invariant interface i.e. member functions.

2. You determine that your type has a state invariant meaning that the representation has range of valid values but clients are allowed to update the representation so to enforce the state invariant you make the representation private and enforce clients to use the interface i.e. member functions to update without putting the instance of that type in an undefined state. If the represenstation is public how could you ever maintain the state invariant from clients? exactly you couldn't clients could easliy set invalid values into public data members.

So to summarize this:



  • encapsulate the variation/variant and provide invariant interface to it <-- important is the first key to abstraction


  • if your type has a state invariant then encapsulate it by making it private and enforce access/update through the interface i.e. member functions, there-for never putting an instance of that type in an undefined state.



If your type has none of these criteria then there is no reason to make it private, your type probably is not a real type and is nothing more than an aggregate/compound data structure.

A very good example of a type that does not encapsulate its data members and nor does it need to is the standard library type std::pair, a pair has 2 members and this will never change it has no state invariant to maintain there-for its members are public.

Also note if your type has lots of small fine grained accessor methods i.e getters/setters then again you've modelled your type at the wrong or mixed the levels of abstraction and you've probably got nothing more than a an aggregate/compound data structure. If its mean't to really represent a proper type then perhaps your type's representation should be grouped into new types.
It's the best advise to give. You can ignore it in many scenerios without serious regret, but sometimes you will severely regret not following it. How severely depends on how doggedly you ignore it. If at the first sign you shouldn't have done that you change it then not too severely. If you are determined to not do it then you can create one hell of a mess when you work around the problems it caused instead of fixing the problem.
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement