lundi 9 octobre 2017

Different types of struct in parameter function

I´m trying to generalize a method in a class. This method receives or a int or a struct and a string. But the struct can be different depending of the result. At the moment I have the following:

struct ResultDetection {
  int someData;
  float score;
  float confidence;
};

struct ResultAttributeBody {
  SomeAnotherStruct data;
  float score;
};

// and some more structs...

class ResultWriter {
  void writeResultIndex(int res, string &message);
  void writeResultDetection(ResultDetection res, string &message);
  void writeResultAttribute(ResultAttributeBody res, string &message);
  // same methods for more structs as parameter
};

I want unify in one method instead of these two or more (because I have more specific structs receiving from a third party code). I tried templates, but it is not working. Polymorphism is a possibility but I would like to avoid subclasses (the code for writing specific methods would be smaller than the subclasses) and also don´t know how I could write a virtual method when one of the parameters varies (the struct).

Any suggestions to avoid duplicating almost the same methods?

Aucun commentaire:

Enregistrer un commentaire