I have a struct, Foo, with a pointer array of Bar.
struct Foo
{
Bar* _BarList;
Foo()
{
_BarList = new Bar[1];
_BarList[0] = Bar(10, 20);
}
Foo& operator=(const Foo& foo)
{
Bar* tmpBarList = new Bar[1];
tmpBarList[0] = foo._BarList[0];
delete[] _BarList;
_BarList = tmpBarList;
return *this;
}
~Foo()
{
delete[] _BarList;
}
};
I call it like this
Foo baz = fooFunc();
(It seems) that if I create an instance of Foo (f) inside a function (fooFunc) and return it, the destructor is called before the value is returned as I lose the contents of _BarList.
This makes sense as it was created within the function. Here is an example.
Foo fooFunc()
{
Foo f = Foo();
return f;
}
If I return an instance of Foo directly on the return, the destructor of that item isn't called until after the equals operator has been called (by the equals statement on the line of the call).
Foo fooFunc()
{
return Foo();
}
I suppose this makes sense as I am creating Foo inside the object, and things are cleared up before the object is returned.
I think I can resolve this by doing the return like this (after writing a new constructor to take Foo):
Foo fooFunc()
{
Foo f = Foo();
return Foo(f);
}
1) Am I right in my assumptions?
2) Is there another way to do it that would not require so many repeated assignment operators being called?
Edit: Please consider that this function would normally do a lot more than just return Foo()!