何时在函数 args 中使用 const 和 const 引用? [英] When to use const and const reference in function args?

查看:40
本文介绍了何时在函数 args 中使用 const 和 const 引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写具有传递给它的 args 的 C++ 函数时,根据我的理解,如果您可以保证对象不会被更改,则应始终使用 const,或者如果指针不会更改,则应始终使用 const 指针.

When writing a C++ function which has args that are being passed to it, from my understanding const should always be used if you can guarantuee that the object will not be changed or a const pointer if the pointer won't be changed.

还有什么时候建议采用这种做法?

When else is this practice advised?

什么时候使用 const 引用?例如,与仅通过指针传递相比有什么优势?

When would you use a const reference and what are the advantages over just passing it through a pointer for example?

这个 void MyObject::Somefunc(const std::string& mystring) 如果字符串实际上已经是不可变对象,那么使用 const 字符串有什么意义?>

What about this void MyObject::Somefunc(const std::string& mystring) What would be the point in having a const string if a string is in fact already an immutable object?

推荐答案

询问是否添加 const 是错误的问题,很遗憾.

Asking whether to add const is the wrong question, unfortunately.

void modifies(T &param);
void modifies(T *param);

这种情况主要是关于样式的:您希望调用看起来像 call(obj) 还是 call(&obj)?但是,有两点差异很重要.如果您希望能够传递空值,则必须使用指针.如果您要重载运算符,则不能改用指针.

This case is mostly about style: do you want the call to look like call(obj) or call(&obj)? However, there are two points where the difference matters. If you want to be able to pass null, you must use a pointer. And if you're overloading operators, you cannot use a pointer instead.

void doesnt_modify(T const &param);
void doesnt_modify(T param);

这是一个有趣的案例.经验法则是廉价复制"类型按值传递——这些通常是小类型(但不总是)——而其他类型则通过 const ref 传递.但是,如果你无论如何都需要在你的函数中制作一个副本,你应该通过按值.(是的,这暴露了一些实现细节.C'est le C++.)

This is the interesting case. The rule of thumb is "cheap to copy" types are passed by value — these are generally small types (but not always) — while others are passed by const ref. However, if you need to make a copy within your function regardless, you should pass by value. (Yes, this exposes a bit of implementation detail. C'est le C++.)

void optional(T const *param=0);
// vs
void optional();
void optional(T const &param); // or optional(T param)

这与上面的非修改情况有关,只是传递参数是可选的.这三种情况之间的差异最小,因此请选择使您的生活最轻松的一种.当然,非常量指针的默认值由您决定.

This is related to the non-modifying case above, except passing the parameter is optional. There's the least difference here between all three situations, so choose whichever makes your life easiest. Of course, the default value for the non-const pointer is up to you.

void f(T);
void f(T const);

这些声明实际上是完全相同的函数!当按值传递时,const 纯粹是一个实现细节.试试看:

These declarations are actually the exact same function! When passing by value, const is purely an implementation detail. Try it out:

void f(int);
void f(int const) {/*implements above function, not an overload*/}

typedef void C(int const);
typedef void NC(int);
NC *nc = &f;  // nc is a function pointer
C *c = nc;  // C and NC are identical types

这篇关于何时在函数 args 中使用 const 和 const 引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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