为什么在每次循环迭代结束时消失我的成员变量?

时间:2019-07-27 03:47:09

标签: c++

在下面的循环中,我从一个向量中获取字符串变量,并将其在构造函​​数中使用,以获得一堆元素,这些元素将添加到另一个构造函数中。

while (std::getline(qt_prim_file_stream, temp_str, '\n')) {
    if (temp_str.empty()) // Blank line implies new module
    {
      //output_wires is a vector of string
      std::string cur_output_wire=output_wires[m_hex_to_SOP.size()];
      SOPExpr expr(current_SOP_string, cur_output_wire);
      m_hex_to_SOP.push_back(expr);
      current_SOP_string.clear();
      i_3++;
      continue;
    }
    current_SOP_string.append(temp_str);
    current_SOP_string.push_back('\n');
    i_2++;
  }

我的问题是m_hex_to_SOP(SOPExpr的向量)中每个SOPExpr中的SOPExpr的第二个成员字符串变量(m_output_wire)在每个循环的末尾一直消失(设置为“”)。为什么这很幸福?第一个成员变量(m_gate_level_netlist)未设置为“”。

这是SOPExpr.h的头文件

#include <string>
#include <vector>
#include <ostream>
#include <iostream>

    class SOPExpr {
        std::string m_gate_level_netlist;
        std::string m_output_wire;
    public:
        const std::string &getMOutputWire() const;
    public:
        SOPExpr(const std::string &m_gate_level_netlist);
        SOPExpr(const SOPExpr &expr);
        SOPExpr();
        SOPExpr(const std::string &mGateLevelNetlist, const std::string &mOutputWire);
        const std::string &getGateLevelNetList() const;
    };

这是类文件:

#include "SOPExpr.h"

SOPExpr::SOPExpr(const SOPExpr &expr) {
  m_gate_level_netlist = expr.getGateLevelNetList();
}
SOPExpr::SOPExpr() {}
const std::string &SOPExpr::getGateLevelNetList() const {
  return m_gate_level_netlist;
}

SOPExpr::SOPExpr(const std::string &mGateLevelNetlist,
                 const std::string &mOutputWire)
    : m_gate_level_netlist(mGateLevelNetlist), m_output_wire(mOutputWire) {}

1 个答案:

答案 0 :(得分:2)

大概将m_hex_to_SOP定义为std :: vector,对吧?

在这种情况下,当您调用m_hex_to_SOP.push_back(expr);您实际上是在调用此副本构造函数:

SOPExpr::SOPExpr(const SOPExpr &expr) {
  m_gate_level_netlist = expr.getGateLevelNetList();
}

不复制m_output_wire。