user1664625
user1664625

Reputation:

Store data in class

I have a class that picks up data from the serial port and I receive the amount of switches a LDR measures just 1 and 0.

Now I would like to store this in a class as long the program runs how can I accomplish this with managed variables?

Note the serial class runs every second so when I create open a class that I use now StoreClass Store

Store.Value = LDR_Value; // LDR_Value is the value from the serial bus

When I do this there always will be a copy of StoreClass be created and that doesn't do the trick.

Please help me out here.

Upvotes: 0

Views: 821

Answers (2)

NullPoiиteя
NullPoiиteя

Reputation: 57312

If you want to accumulate your data in a container, use standard containers. I think std::vector would suffice as a good container for your job:

#include <vector>

using namespace std;

int main()
{
   vector<DataType> dataReceived; //define data container

   DataType buffer; //type of your data
   while(recevingFromDevice) //loop that keeps running as long as you're receiving from your device
   {
      buffer = getDataFromDevice(); //buffer what you receive from the device
      dataReceived.push_back(buffer); //add this buffer to the stack of data
   }
   //now dataReceived is an array with all received data
}

Upvotes: 0

Aesthete
Aesthete

Reputation: 18850

If I understand you correctly you want a container to store all the values received. If you have a class named StoreClass, you can create a vector member and do this:

class StoreClass
{
  public:
    void AddValue(int v) { m_values.push_back(v); }

  private:
    std::vector<int> m_values; // Stores all values in order of arrival.
}

Now you only need one instance of your class:

int main()
{
  StoreClass storage;
  while(StillSerialInput())
  {
    storage.AddValue(GetSerialValue());
  }
}

Upvotes: 1

Related Questions