Thread overview
Does new X() return a pointer or not?
Apr 06
faissaloo
Apr 06
faissaloo
April 06
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
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
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
Thanks alot everyone for your replies, this makes sense now.