www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does new X() return a pointer or not?

reply faissaloo <faissaloo gmail.com> writes:
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
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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
parent faissaloo <faissaloo gmail.com> writes:
Thanks alot everyone for your replies, this makes sense now.
Apr 06 2019
prev sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
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