C++:在函数parameter passed by reference时,如果可能,尽量使其是reference to const

来源:互联网 发布:数据挖掘的就业前景 编辑:程序博客网 时间:2024/04/28 05:26

TOPIC:在函数parameter passed by reference时,如果在使用这个函数过程中,你无需改变参数的值,那么应该(一定)使其具有形式func_type function(const para_type &)

2014.2:C++ primer 5th edition 6.2.3节

也就是说,在函数不需要改变参数的值时,不应该只把paramater的类型定为plain reference(&),而是应该定为reference to const(const &)。


原因是:

①“Using a reference instead of a reference to const unduly limits the type of arguments that can be used with the function.”

    这是说,只声明为reference而不是reference to const将会使本可以成为合理的argument的一些东西不能作为argument传递给参数使用。(例见C++ primer 5th edition 6.2.3节


0 0
原创粉丝点击