how to wrtie destructor for class including a Union

自古美人都是妖i 提交于 2019-12-07 06:05:47

问题


I have a class contain different type of variables like this.

class Field
{
  union DATATYPE
  {
    int intValue;
    double doubleValue;
    char* charValue;
    MyClass* MyClassValue;
  } Value;
  ~Field()
  {
    delete[] Value.charValue;
    delete Value.MyClassValue;
  }
}

This destructor gives error. Since some objects don't have charValue initialized, the attempting to delete it raised error.

来源:https://stackoverflow.com/questions/16025132/how-to-wrtie-destructor-for-class-including-a-union

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!