const函数如何优化程序? [英] How does const after a function optimize the program?

查看:110
本文介绍了const函数如何优化程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过这样的方法:

void SomeClass::someMethod() const;

这个const声明有什么作用,如何帮助优化程序?

What does this const declaration do, and how can it help optimize a program?

编辑

我看到这个问题的第一部分在...之前被问过... BUT ,它仍然不能回答第二部分:这将如何优化程序?

I see that the first part of this question has been asked before... BUT, it still doesn't answer the second part: how would this optimize the program?

推荐答案

如果编译器知道类实例的字段没有通过const成员函数调用修改,则它不必重新加载在const函数调用之前它可能已经保存在寄存器中的任何字段。

If the compiler knows that the fields of a class instance are not modified across a const member function call, it doesn't have to reload any fields that it may have kept in registers before the const function call.

这是指在C ++常见问题中的关于const_cast 的讨论。

This is sort of referred to the in C++ FAQ in the discussion on const_cast.

这篇关于const函数如何优化程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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