Deep Copy a TMap<>

How to do this in UE4c++

I recall from my C++ lessons Shallow Copy and Deep Copy, upon inspecting the data in both maps, the UNDO map is being updated when the normal map is being changed, so Im assumiung the assignment in the function only makes a shallow copy.


UPROPERTY()
		TMap<FName, UUI_MATRIX_RowClass*> MyUI_MatrixRowClassMap;

UPROPERTY()
		TMap<FName, UUI_MATRIX_RowClass*> MyUI_MatrixRowClassMap_UNDO;

void AB_ActorBase::CreateUndoState()
{
	// Remove any previous undo states
        MyUI_MatrixRowClassMap_UNDO.Empty();

	//Copy - This should make complete replica of properties - How to deep copy ???
	MyUI_MatrixRowClassMap_UNDO = MyUI_MatrixRowClassMap;
}

Kinda. If I assume some meaning here - the issue is related to the fact that your TMap contains pointers.

When you copy the data of the map, it’s the pointer that’s copied - not the memory at the address they point to. So, you end up with two maps pointing to the same object instances.

How you handle that is up to you, whether you duplicate the objects and copy them around or store only data. Either way the copy/assignment operator will not be enough here.

ok, I did It manually and it seems to have worked, but I thought there might be an easier way to do it as I have lots of properties in the object to be copied, so it was boring typing all them out.

I seem to remember something about copy constructors, but I dont know enough to be messing with the engines TMap definitions to attempt to do it this way myself.

It’s not something you want to change either way - the default implementation is the correct one, it’s more about this particular example where you also need to duplicate the objects.

For the record though, the engine has it’s own internal Undo/Redo transaction system if this is for non-runtime.