Push_back for a vector

I have a strange problem. I have a vector that I would like to click on objects like this:

vector<DEMData>* dems = new vector<DEMData>();
DEMData* demData = new DEMData();
// Build DEMDATA

dems->push_back(*demData);

There will be several hundred DEMData objects in the vector. The problem is, when this code is finished, all the elements are equal to the last element "dropped" by the vector?

Why are other objects redefined in the vector?

edit:

The DemData class is simple, just a data structure with setters and getters:

    class DEMData
{
private:
    int bitFldPos;
    int bytFldPos;
    const char* byteOrder;
    const char* desS;
    const char* engUnit;
    const char* oTag;
    const char* valType;
    int index;
public:
    void SetIndex(int index);
    int GetIndex() const;
    void SetValType(const char* valType);
    const char* GetValType() const;
    void SetOTag(const char* oTag);
    const char* GetOTag() const;
    void SetEngUnit(const char* engUnit);
    const char* GetEngUnit() const;
    void SetDesS(const char* desS);
    const char* GetDesS() const;
    void SetByteOrder(const char* byteOrder);
    const char* GetByteOrder() const;
    void SetBytFldPos(int bytFldPos);
    int GetBytFldPos() const;
    void SetBitFldPos(int bitFldPos);
    int GetBitFldPos() const;
    friend std::ostream &operator<<(std::ostream &stream, DEMData d);
};

EDIT:

I am reading an XML file and creating DEMData objects based on the attributes of each xml element:

DEMData demData;
  for (i = 0; attr[i]; i += 2)
  {
      if(strcmp(attr[i],"BitFldPos") == 0)
      {
      demData.SetBitFldPos(*attr[i + 1] - '0');
      }
      else if(strcmp(attr[i],"BytFldPos") == 0)
      {
        char* pEnd;
        int tmp = strtol(attr[i + 1],&pEnd,10);
        demData.SetBytFldPos(tmp);
      }
      else if(strcmp(attr[i],"ByteOrder") == 0)
      {
        demData.SetByteOrder(attr[i + 1]);
      }
      else if(strcmp(attr[i],"DesS") == 0)
      {
      demData.SetDesS(attr[i + 1]);
      }
      else if(strcmp(attr[i],"EngUnit") == 0)
      {
        demData.SetEngUnit(attr[i + 1]);
      }
      else if(strcmp(attr[i],"OTag") == 0)
      {
        demData.SetOTag(attr[i + 1]);
      }
      else if(strcmp(attr[i],"ValTyp") == 0)
      {
        demData.SetValType(attr[i + 1]);
      }
      else if(strcmp(attr[i],"idx") == 0)
      {
        char* pEnd;
        int tmp = strtol(attr[i + 1],&pEnd,10);
        demData.SetIndex(tmp);
      }
      //printf(" %s='%s'", attr[i], attr[i + 1]);
  }


  // Insert the data in the vector.
  dems.push_back(demData);
+3
source share
5 answers

Why do you assign a vector to a new one? Why are you highlighting your temporary object with a DEMDatanew one?

A vector , , , , DEMData, , , , , , .

, , , , , - , ctor, - , .

: , DEMData, : ctor, .

char std::string s. int - .

Edit2: , -, , std::map - std::string int.

+6

, , . , DEMData ​​.

DEMData* push_back , , .

+3

, . , demData .

, (char *) . , , "" .

+1

DEMData . , () , , DEMData, , DEMData . DEMData ( ), . , DEMData, (), .

0

, - ... , demData ( ) .

I expect normal C ++ code to look like this:

vector<DEMData>* dems = new vector<DEMData>();
DEMData demData
// Build DEMDATA

dems->push_back(demData);
...
delete dems;

Can you insert the rest of the code? or perhaps the loop that the demData building creates?

-2
source

Source: https://habr.com/ru/post/1733966/


All Articles