# CreateDefaultSubobject and pointers

**URL:** <https://forums.unrealengine.com/t/createdefaultsubobject-and-pointers/120801>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [January 7, 2019, 12:30pm UTC](https://forums.unrealengine.com/t/createdefaultsubobject-and-pointers/120801 "2019-01-07T12:30:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_a037b5a31](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a037b5a31](https://forums.unrealengine.com/u/anonymous_user_a037b5a31)\
**Post date:** [January 7, 2019, 12:30pm UTC](https://forums.unrealengine.com/t/createdefaultsubobject-and-pointers/120801/1 "2019-01-07T12:30:45Z")

</div>

Hello! I’ve been following this [tutorial](https://docs.unrealengine.com/en-US/Programming/Tutorials/PlayerCamera/1) to better understand how UE4 c++ code work and I’ve had some trouble understanding some things with CreateDefaultSubobject and pointers.

in the header file i have

```auto

protected:
       UPROPERTY(EditAnywhere)
       class USpringArmComponent* OurCameraSpringArm;
       class UCameraComponent* OurCamera;

```

in the .cpp file

```auto

       OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
       OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));

```

CreateDefaultSubobject seem to be creating an object and we’re assigning it to where OurCameraSpringArm and OurCamera are pointing? But why aren’t we dereferencing them? thanks guys

---

<div class="post-metadata">

**Author:** ![BrUnO\_XaVIeR](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bruno_xavier/32/1110230_2.png) [@BrUnO\_XaVIeR](https://forums.unrealengine.com/u/BrUnO_XaVIeR)\
**Post date:** [January 7, 2019, 1:47pm UTC](https://forums.unrealengine.com/t/createdefaultsubobject-and-pointers/120801/2 "2019-01-07T13:47:05Z")

</div>

Because the function “CreateDefaultSubobject” returns a pointer to the object created.  
The UObject will be created and managed into the game world, not within your class.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a037b5a31](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a037b5a31](https://forums.unrealengine.com/u/anonymous_user_a037b5a31)\
**Post date:** [January 7, 2019, 4:11pm UTC](https://forums.unrealengine.com/t/createdefaultsubobject-and-pointers/120801/3 "2019-01-07T16:11:27Z")

</div>

So the CreateDefaultSubobject function creates an object AND returns the address-of the object? I was confused because i didn’t know which one it did but it would make sense that it does both.

---

<div class="post-metadata">

**Author:** ![SavvyIndoorsman](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SavvyIndoorsman](https://forums.unrealengine.com/u/SavvyIndoorsman)\
**Post date:** [January 8, 2019, 8:26pm UTC](https://forums.unrealengine.com/t/createdefaultsubobject-and-pointers/120801/4 "2019-01-08T20:26:26Z")

</div>

> [@anonymous\_user\_a037b5a31](#):
>
> So the CreateDefaultSubobject function creates an object AND returns the address-of the object?

Yes. You can think of it that way, though I think it’s more common and perhaps more descriptive to say that it returns a pointer.

By the way, passing objects around by pointers is \*extremely \*common in UE4 C++, to the point that it’s basically _the_ way to do it.

For example, when you call up GetController() on a pawn, that function returns a \*pointer \*to the AController that is currently possessing that pawn. Want to get the name of an actor to print it out for debugging? Call up the AActor::GetDebugName(…) function, which takes a _pointer_ to the actor whose name you want to know. Want to apply damage to several actors in a radius around a location? You can use the UGameplayStatics::ApplyRadialDamage(…) function, which (among other parameters) takes a TArray filled with _pointers_ for which actors to ignore.

So it’s to be expected that functions will use pointers to return objects and to accept objects as parameters. It’s the opposite that would be surprising, at least in UE4 C++.

---

<div class="post-metadata">

**Author:** ![AnotherZach](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AnotherZach](https://forums.unrealengine.com/u/AnotherZach)\
**Post date:** [January 9, 2019, 1:15am UTC](https://forums.unrealengine.com/t/createdefaultsubobject-and-pointers/120801/5 "2019-01-09T01:15:35Z")

</div>

Note that CreateDefaultSubobject should only be used in the constructor, and GetWorld()-\>SpawnActor(MyClass::UStaticClass(), Location, Rotation) should be used elsewhere. Goes something like this, syntax not exact, but enough to know what I’m referring to.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a037b5a31](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a037b5a31](https://forums.unrealengine.com/u/anonymous_user_a037b5a31)\
**Post date:** [January 9, 2019, 10:16am UTC](https://forums.unrealengine.com/t/createdefaultsubobject-and-pointers/120801/6 "2019-01-09T10:16:38Z")

</div>

> [@the\_batch](#):
>
> Yes. You can think of it that way, though I think it’s more common and perhaps more descriptive to say that it returns a pointer.

Thanks that’s really helpful. I jumped from learning c++ to EU4 really quickly so stuff like that sometimes elude me.

I saw that there were a lot of pointers used in Unreal engine but there’s barely any new/delete used, is it because of the garbage collector? Should i worry about not understanding what it does or as long as i don’t use new then the engine will take care of every pointer? Some of the stuff i read in books about pointers said they were to be used carefully but they’re really everywhere in unreal so it got me really wondering.

> [@AnotherZach](#):
>
> Note that CreateDefaultSubobject should only be used in the constructor, and GetWorld()-\>SpawnActor(MyClass::UStaticClass(), Location, Rotation) should be used elsewhere.

Thanks i’ll keep that in mind, it’ll probably save me a lot of trouble because i would have used it outside the constructor for sure.
