On Fri, 20 Oct 2000, Baruch Even wrote:
> On Fri, 20 Oct 2000, Angus Leeming wrote:
>
> > On Fri, 20 Oct 2000, Baruch Even wrote:
> > > [on the topic of returning large objects from a function]
> > >
> > > If it's a large object that has long c-tor time the best method is to
> > > return a const refernce as in: Menu const & expand(Buffer *) const;
> > >
> >
> > But you can't do this if the Menu object is created within the function,
> > because you'll then be referencing something that has gone out of scope.
>
> Indeed, if the object is local to the function this is not a solution and
> the only option is to return a const copy of the object.
>
I know this _feels_ bad, but I think the most efficient implementation
will be to handle the temporary ourselves:
Menu* Menu::expand (Buffer* buf)
{
Menu *resulting_menu = new Menu();
...
// add stuff to the new menu
...
return resulting_menu;
}
Which can be used as:
Menu *tmp = menu.expand();
...
...
delete tmp;
Lior.