Thread overview | ||||||
---|---|---|---|---|---|---|
|
July 17, 2009 Why can't return an object instance by ref? | ||||
---|---|---|---|---|
| ||||
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 Re: Why can't return an object instance by ref? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | 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 Re: Why can't return an object instance by ref? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 Re: Why can't return an object instance by ref? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sam Hu | 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?
|
Copyright © 1999-2021 by the D Language Foundation