任何性能的原因把属性保护/私有? [英] Any performance reason to put attributes protected/private?

查看:137
本文介绍了任何性能的原因把属性保护/私有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学校学到了C ++,但是有一些我不知道的事情,比如编译器可以优化的地方或者什么,似乎我已经知道 inline const 可以提高一点...

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...

如果性能是一个重要的事确实将类属性不 public private protected )允许编译器进行更优化的代码?

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 ?

因为我以前的所有老师都说它更安全或防止不需要或授权的类访问/行为但最后,我想知道如果放置属性不 public 可以限制范围,从而紧固东西。

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...

推荐答案

老师告诉你使用私有和保护隐藏实现,教你隐藏信息,而不是支持可疑的性能优化。尝试考虑一个适当的设计第一和性能第二,在99%的情况下,这将是更好的选择(即使在性能关键情况下)。

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.

这篇关于任何性能的原因把属性保护/私有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆