retrun class object from function

Axel Freyn axel-freyn@gmx.de
Sun Sep 13 11:44:00 GMT 2009


Hello,
On Sat, Sep 12, 2009 at 09:41:54PM -0500, maros wrote:
> Hello,
>
> 	I try to find some information about returning class object from  
> function. I find out one possibility. We must use dynamic heap (new,  
> delete). Is there possibility to return class object which is created as  
> local (in body of function, not global)? If there is no way, why it is  
> prohibited? I find out that in older version there was possibility to  
> define return name, which was using for return class from function, but  
> this functionality was removed.
I'm not sure whether I understand your question correctly - but it is
possible:
Look at the example

class Test{};
Test f()
{
  Test t;
  return t;
}
int main()
{
  Test x = f();
}

It returns the class object created in the function f (OK, to be more
precise: the object t will be destroyed and "x" will be created as a
copy of t).

HTH,

Axel



More information about the Gcc-help mailing list