参数的最佳实践? [英] best practice for parameters?

查看:140
本文介绍了参数的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常认为传递参数作为指针,而不是作为值,当你可以更好?显然,它在很大程度上取决于情况,但是当有一个选择,是否更好使用指针?



这只是为了记忆的原因吗?



如果是真的,指针或引用?

解决方案

一些一般的经验法则:



如果需要修改它,请传递一个指针或引用。如果值可能为null,则传递一个指针,否则传递一个引用。



如果大,传递一个const指针或const引用,



如果使用指针,则更喜欢智能指针指向裸指针。

否则,传递值。 / p>

Is it generally considered better to pass parameters as pointers rather than as value when you can? Obviously it largely depends on the situation, but when there is a choice, is it better to use pointers?

Is this simply for reasons of memory?

And what is better to pass through if it is true, a pointer or a reference?

解决方案

Some general rules of thumb:

If you need to modify it, pass a pointer or a reference. If the value might be null, pass a pointer, otherwise pass a reference.

If it's large, pass a const pointer or const reference, depending on whether null is a legal value.

If using pointers, prefer smart pointers to bare pointers.

Otherwise, pass by value.

这篇关于参数的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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