TSubclassOf get pointer to object of type 'class'

Hi guys,

so I’ve been trying this for a while now and it doesn’t work. So I hope you can help me with that.

	UPROPERTY(EditDefaultsOnly, Category = UMyClass)
	TSubclassOf<class UMyClass> myobjects[LENGTH];

That code is part of a class of mine, it’s editable in the defaults and seems to do what I want it to do.
Now to the problem:

UMyClass* GetMyObject(int32 index) {
    	if (index > -1 || index < LENGTH) 
    		return myobjects[index];
    	return NULL;

The compiler tells me that there is no way to cast from TSubclassOf~UMyClass~ to MyClass*. I have looked into the definition of TSubclassOf and there is an operator that should be able to do that. MyClass extends UObject. (~ used instead of greater and smaller than)

So where is my mistake?

Hi Saniamos,

TSubclassOf<> means ‘a class (UClass) which is a sub-class of the specified type’. So essentially it is a UClass pointer that represents a type derived from the template type (and not an instance of that class). I hope this makes sense.

The objects in your “myobjects” are of type TSubclassOf, whereas you’re trying to return a pointer to an instance of UMyClass. If you’re looking to just access the items in myobject, you will have to change the return type of the function to TSubclassOf. If you are looking for a pointer to an instance of UMyClass, you will need to create one first and then return that.

Yeah, I’ve attached objects of that class to the array in the defaults of my object but how can I get a pointer to them? As I understood TSubclassOf it is a changable reference or pointer to an object that is or extends the given class.

No, it’s a reference to a class derived from the given class (as in reflection type). Not an instance of the class.

Ok, thanks that clarifies it!