Skip to content

Instantly share code, notes, and snippets.

@Seathus
Forked from tenpn/InvisibleCopies.cpp
Last active September 25, 2023 11:30
Show Gist options
  • Save Seathus/73c2cd09b7fde876cf8b80e855738f1b to your computer and use it in GitHub Desktop.
Save Seathus/73c2cd09b7fde876cf8b80e855738f1b to your computer and use it in GitHub Desktop.
struct FWidget
{
FWidget() : Name() {}
FWidget(FString Name) : Name(Name) {}
FWidget(const FWidget& Other) : Name(Other.Name) {}
void ProcessIntensity(float Intensity) const { /** **/ }
FString Name;
}
//we want pass by reference of an existing list not a new copy of the list being passed in so we take an address location for WidgetsToProcess
//the list now contains pointers instead
void ProcessWidgets(const TArray<FWidget>& WidgetsToProcess, float Intensity)
{
//change the auto to FWidget*, we want to avoid a copy being generated in the for loop as a copy can happen during this iterated process
for(const FWidget& Widget : WidgetsToProcess)
{
//widget is now a pointer to . becomes ->
Widget.ProcessIntensity(Intensity);
}
}
//to avoid a copy, we return an address of the newly created widgets array
//the array also needs to be an array of pointers so we're iterating through, we prevent copies.
void CollectWidgets(TArray<FWidget>& Widgets)
{
// collect
}
// returns found index. -1 if index not found
//we want pass by reference of an existing list not a new copy of the list being passed in so we take an address location for WidgetsToProcess
//the list now contains pointers instead
int FindWidget(const TArray<FWidget>& WidgetsToSearch, FString Name)
{
for (int i = 0; i < WidgetsToSearch.size(); ++i)
{
if (WidgetsToSearch[i].Name == Name)
{
return i;
}
}
return -1;
}
void Main()
{
//widgets should contain a list of pointers, pointers we can rely on later on.
const TArray<FWidget> Widgets;
CollectWidgets(Widgets);
ProcessWidgets(Widgets, 99.9f);
int foundWidgetIndex = FindWidget(Widgets, "Find me");
if (foundWidgetIndex != -1)
{
const FWidget& Found = Widgets[foundWidgetIndex];
}
else
{
std::cout << "Widget not found!" << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment