What is the difference between GetClass() and StaticClass()?

For example, if I want to spawn an actor one of input parameters is UClassclass.
I can assign what class to spawn two different ways, by ABomb
BombClass->GetClass() and ABomb::StaticClass()
As I can tell, the first one creates instance, the other doesn’t, can somebody confirm me that?
And what about performance? Which should I use?

1 Like

Hi,

UType::StaticClass() returns the class for UType, whereas Obj->GetClass() returns the class for whichever type Obj was created as, even if you’re calling it through a base pointer.

For example:

AMyActor* ActorPtr = NewObject<AMyActor>(...);
UObject*  ObjPtr   = Actor;

UClass* MyActorClass  = AMyActor::StaticClass(); // AMyActor
UClass* ObjectClass   = UObject::StaticClass();  // UObject
UClass* ActorPtrClass = ActorPtr->GetClass();    // AMyActor
UClass* ObjPtrClass   = ObjPtr->GetClass();      // AMyActor

Hope this helps,

Steve

3 Likes

ahh I see, so UType::StaticClass() is definite, while Obj->GetClass() can be dynamic, since pointer can be reassigned to different object. Thanks man!

yeah i got that, virtual can be overridden by their child class
anyways, thanks man, appreciate it. it can get messy starting with lots of info and not knowing what to implement

That’s how polymorphism and virtual functions work. Suppose we have following classes:

class A{
public:
    int fun(){return 1;}
};
class B : public A{ //B inherits from A
public:
    int fun(){return 2;} //hides A::fun method
};

int main(){
    A* a = new A; 
    A* b = new B;
    B* c = (B*)b;
    a->fun(); //returns 1
    b->fun(); //still returns 1
    c->fun(); //returns 2!
}

But when I add virtual before int fun() in class A body, b->fun() will return 2 instead of 1. Magic! :smiley: