Thread overview | ||||||
---|---|---|---|---|---|---|
|
April 06, 2019 Does new X() return a pointer or not? | ||||
---|---|---|---|---|
| ||||
I have the following function static Component* constructComponent(int value) { return (new ComponentChild(value)); } ComponentChild is a derived class of Component. However, I get told that ComponentChild cannot be converted to Component*. I'm confused here, does new return a heap pointer or not? Are objects automatically assumed to be pointers? |
April 06, 2019 Re: Does new X() return a pointer or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to faissaloo | On Saturday, 6 April 2019 at 13:34:06 UTC, faissaloo wrote: > ComponentChild is a derived class of Component. This means it is not a Component*. new X returns a pointer if X is a struct, but for classes, it is not. > Are objects automatically assumed to be pointers? Yeah, for classes. |
April 07, 2019 Re: Does new X() return a pointer or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to faissaloo | On 07/04/2019 2:34 AM, faissaloo wrote:
> Are objects automatically assumed to be pointers?
It is a reference. Which is a fancy way of saying pointer under the hood.
Just don't do pointer arithmetic with it ;)
Object* means a pointer to a class object reference. Which isn't what you were intending.
int* means a pointer to a 4-byte signed integer.
Does that make sense?
|
April 06, 2019 Re: Does new X() return a pointer or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Thanks alot everyone for your replies, this makes sense now. |
Copyright © 1999-2021 by the D Language Foundation