Thread overview
Why can't return an object instance by ref?
Jul 17, 2009
Sam Hu
Jul 17, 2009
Sam Hu
July 17, 2009
Hi,

With below code:
class A
{
}
class B
{
public:
ref A createA() //  q2:without type name A is also OK?
{
    return new A;//q1
}

}
My questions are:
q1.why this can't work?

q2.I can understand below code works:
A createA(){return new A;}
but why this one also works?
ref createA(){return new A;} // no return type A?


Thanks so much for your help in advance.

Regards,
Sam
July 17, 2009
On Thu, Jul 16, 2009 at 9:47 PM, Sam Hu<samhudotsamhu@gmail.com> wrote:
> Hi,
>
> With below code:
> class A
> {
> }
> class B
> {
> public:
> ref A createA() //  q2:without type name A is also OK?
> {
>    return new A;//q1
> }
>
> }
> My questions are:
> q1.why this can't work?

Because 'new A' is not an lvalue.  Why do you want to return a reference to a class, anyway?  Classes already *are* by reference.
July 17, 2009
Jarrett Billingsley Wrote:
> 
> Because 'new A' is not an lvalue.  Why do you want to return a reference to a class, anyway?  Classes already *are* by reference.

Thank you.I got it.
So how about q2:
but why this one also works?
ref createA(){return new A;} // no return type A?

July 17, 2009
On Fri, Jul 17, 2009 at 8:32 AM, Sam Hu<samhudotsamhu@gmail.com> wrote:
> Jarrett Billingsley Wrote:
>>
>> Because 'new A' is not an lvalue.  Why do you want to return a reference to a class, anyway?  Classes already *are* by reference.
>
> Thank you.I got it.
> So how about q2:
> but why this one also works?
> ref createA(){return new A;} // no return type A?
>
>

I have no idea.  Bug?