# Correct way to cast UInterface of Virtual functions? (NO UFUNCTIONS) C++

**URL:** <https://forums.unrealengine.com/t/correct-way-to-cast-uinterface-of-virtual-functions-no-ufunctions-c/1194492>\
**Category:** C++\
**Tags:** unreal-engine, question, CPP\
**Created:** [June 10, 2023, 1:59am UTC](https://forums.unrealengine.com/t/correct-way-to-cast-uinterface-of-virtual-functions-no-ufunctions-c/1194492 "2023-06-10T01:59:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivan3z](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ivan3z](https://forums.unrealengine.com/u/Ivan3z)\
**Post date:** [June 10, 2023, 1:59am UTC](https://forums.unrealengine.com/t/correct-way-to-cast-uinterface-of-virtual-functions-no-ufunctions-c/1194492/1 "2023-06-10T01:59:23Z")

</div>

have an interface that does not contain UFUNCTIONS

> UINTERFACE()  
> class UMyInteface : public UInterface  
> {  
> GENERATED\_BODY()  
> };

> class IMyInteface  
> {  
> GENERATED\_BODY()
> 
> public:  
> virtual MyFunction();  
> };

1-If I use Cast\< **UClass** \>() I get a pointer to UMyInteface

> UMyInteface\* UPTR = Cast(UObj)

2-If I use Cast\< **Class** \>() get a compile error.

> UMyInteface\* IPTR = Cast\< IMyInteface \>(UObj)

3- If I use dinamic\_cast\< **Class** \>() I get a compile error.

> dynamic\_cast\< **IMyInteface** \>(UObj)

4- If I use cast C style i not sure what will happend.

> if (Owner-\>Implements\< **UMyInteface** \>()) return;  
> IMyInteface \* MyIntefacePtr = ( **IMyInteface** \*)UObj;  
> if (MyIntefacePtr == nullptr) return;

In this case:  
Which is the right way to do it? or the safest way?

Because I suppose that in this case the function must be called like this:

> MyIntefacePtr-\>MyFunction();

and not in this other way.

> IMyInteface::Execute\_MyFunction( UPTR );

Thank you so much!!

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [June 10, 2023, 3:12am UTC](https://forums.unrealengine.com/t/correct-way-to-cast-uinterface-of-virtual-functions-no-ufunctions-c/1194492/2 "2023-06-10T03:12:03Z")

</div>

Posts 5 and 7 might help you

> [@The real problem of blueprint CastTo](https://forums.unrealengine.com/t/the-real-problem-of-blueprint-castto/1185057/5):
>
> Curious, what did you benchmark there? In the editor with all other things running at the same time it’s hard to be accurate… maybe when testing a million runs I don’t know XD. There’s something else going on with interfaces, in c++ you have to do multiple checks to get a proper interface cast depending on if the interface is blueprint only or not. Extremely frustrating. bool bCanBeGrabbed = false; if (OtherOwner-\>Implements\<UGrabInteractionInterface\>()) { bCanBeGrabbed = IGrabInteractionInt…

Can’t tell you how this behaves with no UFUNCTION macro since it might not be able to reflect without one. Normally you declare your functions as UFUNCTION in the IMyInterface.

I believe that the only time you use

```auto
Implements<UGrabInteractionInterface>()

```

is when you need to test a blueprint implementation, but I might be wrong. If a method is not UFUNCTION it can’t be implemented by a blueprint.

Another way is to cast to it

```auto
else if (const IGrabInteractionInterface* OtherInterface = Cast<IGrabInteractionInterface>(OtherOwner)) {
	bCanBeGrabbed = OtherInterface->CanBeGrabbed(InPrimitiveComponent);
}

```

My post 5 on that link gives some info on when and why, but Chatouille in post 7 says things might be different in current versions of the engine.

---

<div class="post-metadata">

**Author:** ![Ivan3z](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ivan3z](https://forums.unrealengine.com/u/Ivan3z)\
**Post date:** [June 10, 2023, 3:18am UTC](https://forums.unrealengine.com/t/correct-way-to-cast-uinterface-of-virtual-functions-no-ufunctions-c/1194492/3 "2023-06-10T03:18:47Z")

</div>

Thank you very much for your help Roy!!  
Very appreciated!!

---

<div class="post-metadata">

**Author:** ![Ivan3z](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ivan3z](https://forums.unrealengine.com/u/Ivan3z)\
**Post date:** [June 10, 2023, 3:27am UTC](https://forums.unrealengine.com/t/correct-way-to-cast-uinterface-of-virtual-functions-no-ufunctions-c/1194492/4 "2023-06-10T03:27:04Z")

</div>

> [@Roy\_Wierer.Seda145](#):
>
> is when you need to test a blueprint implementation, but I might be wrong. If a method is not UFUNCTION it can’t be implemented by a blueprint.

It is not for blueprint… that’s why I don’t use UFUCTION…

But it wouldn’t let me derive from a pure class in C++… it had to be derived from UObject… However, being derived from UObject also has very useful benefits… so it’s fine… but I don’t need UFUCTIONS…

Thank you so much!! ❤

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [July 10, 2023, 3:27am UTC](https://forums.unrealengine.com/t/correct-way-to-cast-uinterface-of-virtual-functions-no-ufunctions-c/1194492/5 "2023-07-10T03:27:52Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
