c++c++11parametersnon-repetitive

How to pass fields from a class to function in c++?


In some words: how can I pass various fields from a custom class to a single function?

Now in details: I have a std::vector containing a class, for example CustomClass from which I have to extract a result from a field from this class by some criteria which are fields in this class and to combine somehow this data.

My first approach to this problem was to use a function which accepts as a parameter the std::vector of the class in order to extract the data and return a std:map. The key in this map is the type of the criteria by which the data should be combined and the value is an int with the combined data from all members of this vector.

The problem is that the criteria is not only one - more than one field from this class may be used as criteria (let for easiness all of the criteria are std::string, if they are not - I could make the function templated).

The easiest way for me now is to make dozens of functions with almost identical code and each of them to extract a simple concrete field from this class. However changes might require similar changes to all of the dozens of functions which would be a maintenance headache. But in this stage I cannot think how to pass to a single function a field from this class...

Here's an example code from this class:

// this is the class with data and criteria
class CustomClass
{
public:
    std::string criteria1;
    std::string criteria2;
    std::string criteria3;
//... and others criteria
    int dataToBeCombined;
// other code
};

// this is one of these functions
std::map<std::string, int> getDataByCriteria1(std::vector<CustomClass> aVector)
{
    std::map<std::string, int> result;
    foreach(CustomClass anObject in aVector)
    {
        if(result.find(anObject.criteria1)==result.end()) // if such of key doesn't exists
        {
            result.insert(std::make_pair(anObject.criteria1, anObject.dataToBeCombined));
        }
        else
        {
            // do some other stuff in order to combine data
        }
    }
    return result;
}

and by similar way I should make the other functions which should work with CustomClass::criteria2, CustomClass::criteria3, etc.

I thought to make these criteria in a single array and to pass to this function only the number of the criteria but the class will be used by others for other purposes and the fields must be easy to read, so this will not be an option (i.e. the real names are not criteria1, criteria2, etc. but are descriptive).

Anyone with ideas?

EDIT: Someone referred my question to "C++ same function parameters with different return type" which obviously is very different - the function in my case return the same type every time, just the parameters it takes must be various fields from a class.


Solution

  • You can use a function to extract a filed such as

    std::string extractFiled(const CustomClass &object, int which) {
      switch (which) {
        case 1:
          return object.criteria1;
        case 2:
          return object.criteria2;
        case 3:
          return object.criteria3;
        default:
          return object.criteria1;
      }
    }
    

    and getDataByCriteria add an arg to indicate which filed to use. Or you can just use macro to implement getDataByCriteria.