I know that it's alright to return by reference if the referenced variable was also passed into the function by reference. For example:
int& foo(int& bar)
{
bar++;
return bar;
}
However, I'm wondering if it's possible to also return by reference if you're returning an object that was created in the function via the new
keyword. I tried something like the following, but got a compiler error:
vector<int>& baz()
{
return new vector<int>();
}
Is there a way to return the new vector by reference that makes sense, or is it better to just return a pointer?