When to pass by reference and when to pass by pointer in C++?
<p>Common Situations:</p>

<ol>
<li>Passing std::string to a function foo(std::string*) or foo(std::string&amp;);</li>
<li>Passing tr1::shared_ptr to a function foo(tr1::shared_ptr* ptr) or foo(tr1::shared_ptr&amp; ptr);</li>
</ol>

<p>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.</p>

<p>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.</p>

<p>Do you think the following snippet is good?</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;map&gt;
#include &lt;string&gt;
#include &lt;tr1/memory&gt;
#include &lt;algorithm&gt;
using namespace std;
using namespace std::tr1;

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

<p>Thanks
Ajay</p>
