# How to switch pointer value between parent and child?

**URL:** <https://forums.unrealengine.com/t/how-to-switch-pointer-value-between-parent-and-child/490587>\
**Category:** Platform & Builds\
**Tags:** parent, Switch, unreal-engine, pointer, question, CPP\
**Created:** [November 4, 2021, 12:05pm UTC](https://forums.unrealengine.com/t/how-to-switch-pointer-value-between-parent-and-child/490587 "2021-11-04T12:05:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NuclearLuca117](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nuclearluca117/32/88711_2.png) [@NuclearLuca117](https://forums.unrealengine.com/u/NuclearLuca117)\
**Post date:** [November 4, 2021, 12:05pm UTC](https://forums.unrealengine.com/t/how-to-switch-pointer-value-between-parent-and-child/490587/1 "2021-11-04T12:05:04Z")

</div>

Hello! I have a function that switches between stances (Earth, Wind, Fire etc). To implement this I have a switch function that swaps the pointer value of ‘CurrentStance’ to the new stance. Here I state the parent/child relationship between the classes:

```
class UBaseStanceComponent : public UActorComponent;
class UAirStanceComponent : public UBaseStanceComponent;
// ...

class AMainCharacter : // ...
{
    UBaseStanceComponent* CurrentStance;
    UAirStanceComponent* AirStance;
   // ...
};

```

Now I assign the pointers to their respective objects:

```
CurrentStance = CreateDefaultSubobject<UBaseStanceComponent>(TEXT("Current Stance"));
AirStance = CreateDefaultSubobject<UAirStanceComponent>(TEXT("Air Stance"));

```

Finally we have the switching function that changes the pointer of CurrentStance to the new stance:

```
void AMainCharacter::ChangeStance(EStance Stance)
{
	switch(Stance)
	{
		case EStance::AirStance:
			CurrentStance = AirStance;
			break;

```

The penultimate line causes the following error:

```
Cannot assign to type 'UBaseStanceComponent*' from 'UAirStanceComponent*'

```

However surely this should be fine given Air inherits from UBaseStanceComponent? Any help would be greatly appreciated. Thank you!

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [November 5, 2021, 4:52pm UTC](https://forums.unrealengine.com/t/how-to-switch-pointer-value-between-parent-and-child/490587/2 "2021-11-05T16:52:47Z")

</div>

Hello! If you want to use CurrentStance for switching, you dont need this line

```
CurrentStance = CreateDefaultSubobject<UBaseStanceComponent>(TEXT("Current Stance"));

```

just set it to nullptr or default stance…

---

<div class="post-metadata">

**Author:** ![NuclearLuca117](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nuclearluca117/32/88711_2.png) [@NuclearLuca117](https://forums.unrealengine.com/u/NuclearLuca117)\
**Post date:** [November 8, 2021, 12:12pm UTC](https://forums.unrealengine.com/t/how-to-switch-pointer-value-between-parent-and-child/490587/3 "2021-11-08T12:12:07Z")

</div>

Thanks for the reply! However, I don’t think this is a solution, because say I do initalise CurrentStance to nullptr, when it comes to setting the pointer to Air I still have the same problem.

---

<div class="post-metadata">

**Author:** ![MyxaWW](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MyxaWW](https://forums.unrealengine.com/u/MyxaWW)\
**Post date:** [November 8, 2021, 9:21pm UTC](https://forums.unrealengine.com/t/how-to-switch-pointer-value-between-parent-and-child/490587/4 "2021-11-08T21:21:53Z")

</div>

It seems like you should be able to. Is there a particular reason you don’t just

```
  Cast<UBaseStanceComponent>(Airstance)

```

?
