# C++ Interface Cast returns NULL when implemented with Editor

**URL:** <https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784>\
**Category:** UI\
**Tags:** interface, unreal-engine, Code, null, CPP, Blueprint, question\
**Created:** [October 9, 2015, 4:54pm UTC](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784 "2015-10-09T16:54:34Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Angel\_Phosphoros](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Angel\_Phosphoros](https://forums.unrealengine.com/u/Angel_Phosphoros)\
**Post date:** [March 7, 2019, 1:01pm UTC](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784/6 "2019-03-07T13:01:17Z")

</div>

I wrote very clear way to do this.

Snippet from here: [Interface implementation check for Unreal Engine 4 (UE4) ($1832992) · Snippets · District Zero / Unreal Engine 4 Snippets · GitLab](https://gitlab.com/district0/unreal-engine-4-snippets/snippets/1832992)

Code:

```
#pragma once

#include "Interface.h"

/**
* Fast and full checking of interface implementation including nullptr check, C++ cast check and blueprint check
* @param TIInterfaceType is interface IClass (not UClass)
* @param object is pointer to check
* @return true if object implements TIInterfaceType, false if nullptr or doesn't implement
*/
template<typename TIInterfaceType>
FORCEINLINE bool IsInterfaceImplementedBy(const UObject* const object)
{
    return 
        // Nullptr is invalid
        object && 
        // C++-style cast much faster
        (Cast<TIInterfaceType>(object) || 
        // But blueprint implementation pass only this check
        object->GetClass()->ImplementsInterface(TIInterfaceType::UClassType::StaticClass()));
}

```

You need to include this header into every interface declaration and use it in C++:

```
void ATestCodeGameModeBase::CallDoSomething(UObject* object) 
{
    // Clear check of what we mean
    if (IsInterfaceImplementedBy<ITestInterface>(object))
    {
        ITestInterface::Execute_DoSomething(object);
    }
    else
    {
        UE_LOG(LogTemp, Error, TEXT("Object does not implement interface"));
    }
}

// vs

void ATestCodeGameModeBase::CallDoSomething(UObject* object)
{
    // Doesn't understandable
    if (object && object->GetClass()->ImplementsInterface(UTestInterface::StaticClass()))
    {
        ITestInterface::Execute_DoSomething(object);
    }
    else
    {
        UE_LOG(LogTemp, Error, TEXT("Object does not implement interface"));
    }
}
```

---

_[View the full topic](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784)._
