Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any performance reason to put attributes protected/private?

I "learned" C++ at school, but there are several things I don't know, like where or what a compiler can optimize, seems I already know that inline and const can boost a little...

If performance is an important thing (gaming programming for example), does putting class attributes not public (private or protected) allow the compiler to make more optimized code ?

Because all my previous teacher were saying was it's more "secure" or "prevent not wanted or authorized class access/behavior", but in the end, I'm wonder if putting attributes not public can limit the scope and thus fasten things.

I don't criticize my teachers (should I), but the programming class I was in wasn't very advanced...

like image 860
gokoon Avatar asked Dec 23 '22 01:12

gokoon


2 Answers

The teachers were right to tell you to use private and protected to hide implementation and to teach you about information hiding instead of propsing questionable performance optimizations. Try to think of an appropriate design first and of performance second, in 99% of the cases this will be the better choice (even in performance critical scenarios). Performance bottlenecks can appear in a lot of unpredicted cases and are much easier to come by if your design is sound.

To directly answer your question however: any reduction in scope may help the compiler to do certain optimizations, form the top of my head I can not think of any however right now in regards to making members private.

like image 113
Janick Bernet Avatar answered Jan 23 '23 08:01

Janick Bernet


No. Making members private or protected is not going to provide any performance benefits; of course, the benefits to your design (information hiding) are huge.

like image 28
Martin B Avatar answered Jan 23 '23 07:01

Martin B