在这种情况下,工会成员会调用自己的析构函数吗

Will the members of a union member call their own destructors in this scenario?

本文关键字:自己的 析构函数 调用 成员 这种情况下      更新时间:2023-10-16

当从联合中显式调用具有空dtor的类时,该类会调用其成员的dtor吗?

这有点难以形容,希望psuedo代码更直接。在本例中,Texture::~Texture()是否会隐式调用source.bitmap.pixels.~vector()

struct Bitmap{
~Bitmap(){} // empty dtor
// members
std::vector<uint8> pixels;  // <-- will this dealloc when ~Bitmap() is called manually?
};
struct Texture{
~Texture(){
// assume sourceType is 1
switch(sourceType){ 
case 1:
source.bitmap.~Bitmap();
break;
}
}
// members
uint sourceType;
union Source{
Source(){}
~Source(){}
// members
Bitmap bitmap;
}source;
};

析构函数的执行定义为执行析构函数函数体,然后执行对成员和基的析构函数调用。伪析构函数调用执行析构函数。

因此,是的,在这种情况下,伪析构函数调用将正确地销毁pixels

是的,它将隐式调用所有成员的析构函数,如果您的类是从另一个类派生的,它也将调用基类的析构因子。