digitalmars.D.learn - Why can't return an object instance by ref?
- Sam Hu (22/22) Jul 16 2009 Hi,
- Jarrett Billingsley (3/18) Jul 16 2009 Because 'new A' is not an lvalue. Why do you want to return a
- Sam Hu (5/8) Jul 17 2009 Thank you.I got it.
- Jarrett Billingsley (2/10) Jul 17 2009 I have no idea. Bug?
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
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
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
On Fri, Jul 17, 2009 at 8:32 AM, Sam Hu<samhudotsamhu gmail.com> wrote:Jarrett Billingsley Wrote:I have no idea. Bug?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?
Jul 17 2009