Issue with dereferencing and = operator

I was working with a TArray of pointers and I had some trouble. The TArray is of size 1000 and my goal is to copy the first 500 to the second 500. Originally I did it like this

for (int i = 500; i < 1000; i += 1) {
    CreatureList[i] = CreatureList[i - 500];
    CreatureList[i]->Mutate();
}

Here CreatureList is a TArray, but since an element of CreatureList is a pointer, its passing that pointer and when the function Mutate is called, both the creature at index i and index i - 500 reflect the changes of the Mutate function.

Note: The elements of CreatureList must be pointers because I need them to be polymorphic and from what I understand polymorphism only works with pointers in c++ (Correct me if I’m wrong about that)

I discovered a solution outside of unreal engine which worked perfectly fine.

for (int i = 500; i < 1000; i += 1) {
    *CreatureList[i] = *CreatureList[i - 500];
    CreatureList[i]->Mutate();
}

But when I use this syntax in unreal engine I get an error saying “UCreature &UCreature::operator=(const UCreature &) declared at line 27 of Creature.h is inaccessible”
In my header file, line 27 is

GENERATED_BODY()

Does anybody have any insight into why I’m not allowed to use this operator here? If there are any alternatives or workarounds, that would be great as well. I’m making an evolution simulator, and if every creature is mutating instead of a specific one in the population, then that would cause problems. Thanks!

Try replacing line:2 with CreatureList[i] = DuplicateObject(CreatureList[i-500], outer, newName)

(See UObjectGlobals.h for reference.)

Instantiating an UObject has too many things happening under the hood. That’s why they overload the operator “=” to prevent users from copying by themselves.

@madturtleGGinin Thank you. I think that worked but it’ll be a little bit of time before I can check it since I have to work through the rest of the basic project architecture. So I wrote the DuplicateObject function like this->

DuplicateObject<UCreature>(CreatureList[i-500], this);

and I wanted to check something before I procede. Does this guarantee that if CreatureList[i-500] is a type that is a subclass of UCreature, does that mean itll duplicate it as that subclass? or will the template argument < UCreature> make it copy over as a UCreature only?

The return value is UCreature but you should be able to cast it to its subclass like

UDog* dog = Cast<UDog>(CreatureList[i]);

Is that what you’re asking? Or do you have specific task you need to achieve?

Yeah I think so. To clarify my question further, I mean that if UDog is a subclass of UCreature and I do this:

UDog* dog = NewObject<UDog>();
UCreature* creature = DuplicateObject<UCreature>(dog, this);

will the variable creature be a dog since its duplicating a UDog pointer? or will dog be duplicated as a UCreature since that’s the template argument of the DuplicateObject function?