Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to pass by reference and when to pass by pointer in C++?

Tags:

c++

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

like image 368
user855 Avatar asked Aug 31 '10 20:08

user855


People also ask

When should I use references and when should I use pointers?

Use references when you can, and pointers when you have to. References are usually preferred over pointers whenever you don't need “reseating”. This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.

When should you pass a pointer?

You pass a pointer to pointer as argument when you want the function to set the value of the pointer. You typically do this when the function wants to allocate memory (via malloc or new) and set that value in the argument--then it will be the responsibility of the caller to free it.

Why would we want to pass a pointer by reference?

You would want to pass a pointer by reference if you have a need to modify the pointer rather than the object that the pointer is pointing to. This is similar to why double pointers are used; using a reference to a pointer is slightly safer than using pointers.

Is pointer passed by reference C?

There is no such thing as reference in C. Passing a pointer to a function will not copy the object that the pointer is pointing to.


2 Answers

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.

like image 111
David Thornley Avatar answered Oct 04 '22 01:10

David Thornley


A good rule of thumb: "Use references when you can and pointers when you have to".

like image 40
Lucas Avatar answered Oct 04 '22 00:10

Lucas