Search code examples
c++if-statementpointersunreal-engine4

should I check pointers in such situations?


I'm new to programming and game development in general every time I read and hear that pointers are a nightmare I want to ask if it is necessary to check pointers in such cases as shown below?

// create a component of a certain type and return a pointer to data of this type
StaticMeshCompt = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
if (StaticMeshCompt)
{
   // further work with the component
}

Solution

  • No, there's no need to check the pointer. The cases where CreateDefaultSubobject could return nullptr are :

    • The class passed is nullptr (It's guaranteed to be valid for any UObject).
    • The class has the abstract flag (Not the case for UStaticMeshComponent).
    • Allocation itself fails due to lack of free memory (At that point, you've got other problems).