www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why can't return an object instance by ref?

reply Sam Hu <samhudotsamhu gmail.com> writes:
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
Jul 16 2009
parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
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() // =A0q2:without type name A is also OK?
 {
 =A0 =A0return 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.
Jul 16 2009
parent reply Sam Hu <samhudotsamhu gmail.com> writes:
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?
Jul 17 2009
parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
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. =A0Why do you want to return a
 reference to a class, anyway? =A0Classes 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?
Jul 17 2009