Suppose I have a class
class Foo
{
public:
~Foo() { delete &_bar; }
void SetBar(const Bar& bar)
{
_bar = bar;
}
const Bar& GetBar() { return _bar; }
private:
Bar& _bar;
}
And my usage of this class is as follows (assume Bar has a working copy constructor)
Foo f;
f.SetBar(*(new Bar));
const Bar* bar = &(f.GetBar());
f.SetBar(*(new Bar(bar)));
delete bar;
I have a situation similar to this (in code I didn't write) and when I debug at a breakpoint set on the "delete bar;" line, I see that
&f._bar == bar
My question is this: Why do &f._bar and bar point to the same block of memory, and if I leave out the "delete bar;", what are the consequences, from a memory management standpoint?
Many thanks!