什么时候通过引用和何时通过指针在C ++? [英] When to pass by reference and when to pass by pointer in C++?

查看:149
本文介绍了什么时候通过引用和何时通过指针在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

常见情况:


  1. 将std :: string传递给函数foo(std :: string *)或foo string&);

  2. 将tr1 :: shared_ptr传递给函数foo(tr1 :: shared_ptr * ptr)或foo(tr1 :: shared_ptr& ptr);

一般来说,什么是好的做法。我总是困惑。最初,将所有内容作为引用看起来是一致的,但是不可能将引用或NULL作为引用传递给文本。

同样,将所有内容作为指针似乎很好,但是我必须担心指针可能指向NULL并检查那个函数开始的那些条件。



你认为下面的代码段是好的吗?

  #include< iostream> 
#include< vector>
#include< map>
#include< string>
#include< tr1 / memory>
#include< algorithm>
using namespace std;
using namespace std :: tr1;

int main(){
map< string,shared_ptr< vector< string> > > adjacencyMap;
vector< string> * myFriends = new vector< string>();
myFriends-> push_back(string(a));
myFriends-> push_back(string(v));
myFriends-> push_back(string(g));
adjacencyMap [s] = shared_ptr< vector< string> >(myFriends);
return 0;
}

感谢
Ajay



你的文字的问题,你没有使用const引用?您不能将临时(由文字生成)绑定到非const引用,因为更改它没有意义。你可以将一个绑定到一个const引用。



特别是,当一个参数传递给一个函数时,函数不会改变它,内置类型,通过const引用。除了不需要复制构造函数调用之外,它的工作方式与传递值相同。



指针有用,因为它们具有保证的无效值,对于。有时这是不相关的,有时是非常重要的。当然,你通常不能通过指针传递一个文字,除非(在字符串文本的情况下)它已经是。



一些编码标准说,由非const引用传递,因为它在调用点不提供参数可能被函数改变的指示。在这种情况下,你将需要通过指针。我不喜欢这一点,特别是当编程工具使得更容易和更容易获得函数签名,所以你可以看到一个函数是否可能改变一个参数。但是,当在组或企业中工作时,样式一致性比任何单独的样式元素更重要。


Common Situations:

  1. Passing std::string to a function foo(std::string*) or foo(std::string&);
  2. Passing tr1::shared_ptr to a function foo(tr1::shared_ptr* ptr) or foo(tr1::shared_ptr& ptr);

In general, what is a good practice. I always get confused. At first, passing everything as references seems consistent, however it is not possible to pass in Literals as references or NULLs as references.

Similarly, having everything as pointers seems good, but having then I have to worry that pointers might be pointing to NULL and check for those conditions in the beginning of that function.

Do you think the following snippet is good?

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <tr1/memory>
#include <algorithm>
using namespace std;
using namespace std::tr1;

int main(){
        map<string, shared_ptr<vector<string> > > adjacencyMap;
        vector<string>* myFriends = new vector<string>();
        myFriends->push_back(string("a"));
        myFriends->push_back(string("v"));
        myFriends->push_back(string("g"));
        adjacencyMap["s"] = shared_ptr<vector<string> >(myFriends);
        return 0;
}

Thanks Ajay

解决方案

References are easier to get right.

Is your problem with literals that you aren't using const references? You can't bind a temporary (produced by a literal) to a non-const reference, because it makes no sense to change one. You can bind one to a const reference.

In particular, when passing an argument to a function, and the function isn't going to change it, and it isn't a built-in type, pass by const reference. It works much the same as pass by value, except it doesn't require a copy constructor call.

Pointers are useful in that they have a guaranteed invalid value you can test for. Sometimes this is irrelevant, and sometimes it's very important. Of course, you can't generally pass a literal by pointer, unless (in case of a string literal) it already is.

Some coding standards say that nothing should ever be passed by non-const reference, since it provides no indication at the point of call that the argument might be changed by the function. In that case, you will be required to pass by pointer. I don't favor this, particularly as programming tools make it easier and easier to get the function signature, so you can see if a function might change an argument. However, when working in a group or for an enterprise, style consistency is more important than any individual style element.

这篇关于什么时候通过引用和何时通过指针在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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