In c++, where should constant strings be placed?

Sure that’s fine. In a header which you include in multiple cpp files:

// Declare the constant string:
extern const FString SomeString;

In a single cpp file:

#include "SomeString.h"
// Define the constant string:
const FString SomeString = "Something";

In any cpp file:

#include "SomeString.h"

// Use the constant string:
void Test(const TMap<FString, UObject*>& Map){
  UObject* obj = Map[SomeString];
}

Some other tips:

  • Pass TMap (and other containers like TArray) by reference, like const TMap<...> & Param or TMap<...> & Param if you want to modify it. Otherwise you’re copying the container every time you pass it as an argument to a function.
  • Use FName rather than FString for these. It’s much faster to compare names than strings and they’re a natural fit for TMap keys for this reason.
2 Likes