复制非琐碎的构造函数和匿名联合

时间:2018-11-16 16:58:31

标签: constructor initialization union copy-constructor

我第一次尝试使用工会,我在编程中有点菜鸟。我正在使用Qt 5.11,并且在编译时出错。我还阅读了有关如何为联合和结构定义构造函数的文章,但我仍然无法使我的代码正常工作。

在“ include.h”中我定义了:

// my struct n°1
struct ELEM_JCC_TO {
  QPair <QString, QString> ID;  // jump destination label
};

// my struct n°2, extension of n°1
struct ELEM_CALLS_TO : ELEM_JCC_TO {

  int VarStackOut;
  int VarStackOutSR;  // exit variation with ret skipping

};

// my main structure
struct ELEMENTO_STACK {

  // type of element, union selector
  enum TYPE_STACK {
    undef,
    Oper,
    Jcc,
    Call,
    Loop
  } Type;

  // anonymous union
  union {

    int VarStack;
    struct ELEM_JCC_TO JccTo;
    struct ELEM_CALLS_TO CallsTo;
    struct ELEM_JCC_TO LoopTo;
  };
};

然后当我简单地声明时在“ main.cpp”中:

...
struct ELEMENTO_STACK El;
...

我的编译窗口几乎变成红色,出现5个编译错误和29个问题:

1。 ... \ qlist.h:494:错误:使用已删除的函数'ELEMENTO_STACK ::〜ELEMENTO_STACK()' while(从!=到-)--to,删除reinterpret_cast(to-> v); ... include.h:519:'ELEMENTO_STACK ::〜ELEMENTO_STACK()'被隐式删除,因为默认定义格式不正确:

2。 ... include.h:533:错误:工会成员'ELEMENTO_STACK :::: JccTo'与平凡的'ELEM_JCC_TO ::〜ELEM_JCC_TO()'

  1. 和4.类似2。

5。 ... \ qlist.h:496:错误:使用已删除的函数'ELEMENTO_STACK ::〜ELEMENTO_STACK()'

我知道我必须处理构造函数并正确初始化所有这些东西。但我不明白如何。我可以得到一些提示让我的代码正常工作吗?非常感谢

****编辑****

经过很多尝试,我写了一些必要的东西让它完成任务。 主要结构'ELEMENTO_STACK'需要定义的构造函数和析构函数,例如

      // default constructor
  /* initialization list initializes the enum type and the union to one 
     of it's member, in this case to "ELEM_JCC_TO()"
  ELEMENTO_STACK() : Type(undef),ELEM_JCC_TO() {}

  // default destructor
  ~ELEMENTO_STACK() {
    switch(Type) {
    case TYPE_STACK::Jcc:
    case TYPE_STACK::Loop:
      ELEM_JCC_TO.~ELEM_JCC_TO();
      break;
    case TYPE_STACK::Call:
      CallsTo.~ELEM_CALLS_TO();
      break;
    case TYPE_STACK::undef:
    case TYPE_STACK::Oper:
      VarStack = 0;
      break;
    }
    Type = undef;
  }

沉迷于此,我在QList中使用了此结构,定义为,

QList <struct ELEMENTO_STACK> Operaz;

要使其正常工作,我需要添加“复制构造函数”和“运算符的构造函数=”,其中必须根据联合中包含的数据类型正确初始化内存,例如:

  // default operator =
  void operator = (const ELEMENTO_STACK &p) {
    Type = p.Type;
    switch(Type) {
    case undef:
    case Oper:
      VarStack = p.VarStack;
      break;
    case Call:
      /* next line is the most important, it initializes memory at 
         location of "CallsTo", calling it's constructor. Whitout
         this initialization, copy something to unknown destination
         will cause unpredictable results, and maybe segmentation fault
      */
      new (&CallsTo) ELEM_CALLS_TO();
      CallsTo.VarStackOut = el.CallsTo.VarStackOut;
      CallsTo.VarStackOutSR = el.CallsTo.VarStackOutSR;
      CallsTo.ID = el.CallsTo.ID;
      break;
    case Jcc:
      // initializes JccTo with it's own constructor 
      new (&JccTo) ELEM_JCC_TO();
      JccTo.ID = el.JccTo.ID;
      break;
    case Loop:
      // initializes LoopTo with it's own constructor
      new (&LoopTo) ELEM_JCC_TO();
      LoopTo.ID = el.LoopTo.ID;
      break;
    }
  }

  // default copy constructor, calls "operator ="
  ELEMENTO_STACK(const ELEMENTO_STACK &p) {*this = p; }

将使用这些上瘾的代码进行编译。

0 个答案:

没有答案