结构,而不是清除/初始化所有数据

Structs and not clearing / initializing all data

本文关键字:初始化 数据 清除 结构      更新时间:2023-10-16

好的,所以我想我知道我需要做什么,但我不确定如何做。

我有这个结构

struct GItemInfo{
string icon = "";
string name = "";
string subname = "";
string rarity = "";
vector<string> ToolTip;
string ToolTipFull;
bool canuse = true;
int depth = 0;
int level = -1;
int leveladjusted = -1;
int slotnum = -1;
bool isslotlocked = true;
int isslothighlighted = 0;
FVector location;
FEntityId id;
float distancetome = 0.0f;
bool istrash = false;
bool ispickuptrash = false;
bool ispickup = false;
bool empty = true;
bool operator<(const GItemInfo& a) const
{
    return distancetome < a.distancetome;
}
};

我比使用

vector<GItemInfo> CurrentGearList;

要初始化它…问题是…即使在我做之后

CurrentGearList.clear();
CurrentGearList.resize(0); // Yes i know some of this is voodoo redundant.
CurrentGearList.resize(20); 

ID内部的值仍然存在,并且永远不会重新初始化。因此,ID结构体只是(int,int(。。如何告诉GItemInfo结构将它们初始化为0,0。

我尝试了FEntityId id={0,0};不喜欢这样。

所以我确信我需要以某种方式在structs构造函数中工作,我只是不确定如何做到。

struct FEntityID{
    int A;
    int B;
    FEntityID(): A(0), B(0){  
    }
};

通过每次创建FEntityID的对象时为其创建默认构造函数,其int值将用0初始化。

如果您想控制如何为每个实例初始化结构对象,还应该为它们定义构造函数。