C++ question about setting class variables
<p>I'm not new to programming, but after working in Java I'm coming back to C++ and am a little confused about class variables that aren't pointers.  Given the following code:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;map&gt;

using namespace std;

class Foo {
    public:
        Foo() {
            bars[0]     = new Bar;
            bars[0]-&gt;id = 5;
        }

        ~Foo() { }

        struct Bar {
            int id;
        };

        void set_bars(map&lt;int,Bar*&gt;&amp; b) {
            bars = b;
        }

        void hello() {
            cout &lt;&lt; bars[0]-&gt;id &lt;&lt; endl;
        }

    protected:
        map&lt;int,Bar*&gt; bars;
};

int main() {
    Foo foo;
    foo.hello();

    map&lt;int,Foo::Bar*&gt; testbars;
    testbars[0]     = new Foo::Bar;
    testbars[0]-&gt;id = 10;

    foo.set_bars(testbars);

    foo.hello();

    return(0);
}
</code></pre>

<p>I get the expected output of 5 &amp; 10.  However, my lack of understanding about references and pointers and such in C++ make me wonder if this will actually work in the wild, or if once testbars goes out of scope it will barf.  Of course, here, testbars will not go out of scope before the program ends, but what if it were created in another class function as a function variable?  Anyway, I guess my main question is would it better/safer for me to create the bars class variable as a pointer to the map map?</p>
