digitalmars.D.learn - Does new X() return a pointer or not?
- faissaloo (8/8) Apr 06 2019 I have the following function
- Adam D. Ruppe (5/7) Apr 06 2019 This means it is not a Component*.
- faissaloo (1/1) Apr 06 2019 Thanks alot everyone for your replies, this makes sense now.
- rikki cattermole (7/8) Apr 06 2019 It is a reference. Which is a fancy way of saying pointer under the hood...
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?
Apr 06 2019
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.
Apr 06 2019
Thanks alot everyone for your replies, this makes sense now.
Apr 06 2019
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?
Apr 06 2019