1

I have 10 independent data structures that contain unsigned bytes and bit fields from different data sources. I am using C++, and was looking for a way to combine all these data structures into one data structure for the purpose of passing all the data around between different methods.

For example I have an object that uses data from all 10 data structures, but I have references similar to:

method(datastructure1 &d1, datastructure &d2, datastructure &d3, datastructure &d4, ...)

I want to avoid this, because it is getting hard to keep track and maintain. I would prefer to have:

method(superStructure &allData);

I just cant think of how to combine all the data so I can still uniquely access the data from each individual structure.

  • Why not just create the containter which will hold all the other data structures as private member variables? (Maybe even in some kind of containers, such as std::vector<dataStructure> v.) – Andy Sep 10 '15 at 15:06
  • Each DataStructure is different than the next one. – zacharoni16 Sep 10 '15 at 16:15
  • 1
    I said private member variables, not just one, many of them. You can encapsulate any data type or data structure you want into a big chunk. Or they could be public in case of using struct. Depends whether you want to hide the implementation and manipulate the data only using public interface. – Andy Sep 10 '15 at 16:26
1

You can do the following:

  1. Create a super class of the 10 data structures. The super class doesn't need to contain any functionality but it will need to contain the name of each one of the functions of the 10 data structures that you want to later use.
  2. Implement your 10 data structures as sub classes of the previously created super class.
  3. Count how many data items you want to send to the method you mentioned in your question and create an array with that size using the super class data type and pass the data array to the method.
  4. Iterate over the array to carry out your operations.

If the function names for the 10 data structures are different you can create a variable in the super class called current_data_type to test which object is being processed in a conditional statement in step 4 from the array of objects passed.

This is an example of how the code would be structured:

    class SuperClass {
      public:

        static int current_data_type;

        // add all data type specific functions
        void set_values_d1 (int,double);
        void set_values_d2 (int,float);
        // ... add as many as necessary
        double get_values_d1 (void);
        float get_values_d2 (void);
        // ... add as many as necessary
    };

    class DataType1: public SuperClass {
      public:
        void set_values_d1 (int,double) { /* do something */ }
        double get_values_d1 (void) { /* do something */ }
        // ... add all the other functions
    };
    int DataType1::current_data_type = 1;

    class DataType2: public SuperClass {
      public:
        void set_values_d2 (int,float) { /* do something */ }
        float get_values_d2 (void) { /* do something */ }
        // ... add all the other functions
    };
    int DataType2::current_data_type = 2;

    int arrayItems = 100;
    SuperClass myDataArray [arrayItems] = {dataItem1, dataItem2, ... , dataItem100};

    method(SuperClass &myDataArray, int arrayItems) {
      for (int i = 0; i < arrayItems; i++) {
        switch(myDataArray.current_data_type){
          case 1 :
            // do all computations for DataType1
            break;

          case 2 :
            // do all computations for DataType2
            break;

          // create as many case statements as there are data types.
        }
      }
    }
  • This is the answer I've been looking for, because each data structure isn't the same type as the other ones, they are all different – zacharoni16 Sep 11 '15 at 17:33
5

Well, that's exactly the purpose of a struct so why not use one ? A struct, in essence, is just a grouping of other data types, and thus it doesn't need memory for itself. It is only a convenience thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.