我联合删除哪个元素有关系吗?

问题描述 投票:0回答:1
我正在编写翻译,为此,我想创建一个Verb类。此类仅应在不规则的情况下保存完整的查询,因为它将产生大量列表,占用大量内存。所以这是我的代码:

#include "string.h" struct Irregular{ std::string present; std::string simplepast; std::string pastparticiple; } union Verbform{ Irregular* irregular; std::string* regular; Verbform(Irregular irreg){irregular=new Irregular(irreg);} Verbform(std::string s){regular=new std::string(s); ~Verbbform(){delete regular;} //here is my problem } class Verb{ public: //some public functions private: Verbform verbform; //some other things; }

[当我这样做并以不规则动词对其进行初始化时,他会删除完整的不规则动词还是仅删除第一个字符串? 

[当我这样做:~Verbform(){delete irregular;}并用普通字符串初始化它时,他删除的内容是否超过我要他删除的内容?

我正在编写翻译,为此,我想创建一个Verb类。此类仅应在不规则的情况下保存完整的查询,因为它会产生较大的列表,占用大量...

c++ union destructor
1个回答
5
投票
delete irregular调用~Irregular()析构函数,然后调用3次std::string::~string()析构函数。当irregular为n而不是活动的联合成员时,调用此方法为

undefined behavior

© www.soinside.com 2019 - 2024. All rights reserved.