0

I have two structs like this

struct Activity {
int id;
string Description;
int parameter1;
int parameter2;
int parameter3;
int parameter4;
etc...
}

Activity A1;
A1.id=0;
A1.parameter1=50;

Activity A2;
A2.id=0;
A2.parameter1=55;

I would like to compare them, to show what members are different ? In this case something like :

paameter1 is different...

Thanks

Gpouilly
  • 1
  • 1

1 Answers1

3

The best solution to do this may be to write public method inside the structure which will compare themselves with structure passed through parameter.

This will look like this

struct Activity {
  int id;
  string Description;
  int parameter1;
  int parameter2;
  int parameter3;
  int parameter4;
  etc...

  public:
    bool compare(const Activity& param)
    {
      //...compare structs here like:
      if (id != param.id)
        //write something

      //etc...

      //at the end you can also return bool that indicates that structs are equal or not
      return true;
    }
}

This will obviously work only with two the same classes unless you write more comparison methods, but it may be difficult to compare two different structures.

There is also other way to compare two variables (including structs). For this a memcmp() function can be used, but it will not tell you directly, which fields are different.

Edited according to what @Tony_D said.

Skorek
  • 323
  • 2
  • 12
  • 1
    +1, but should probably be `bool compare(const Activity& param) const` (if bothering to track a boolean result). The `const &` avoids unnecessarily slow copy-construction of `param`, and the trailing `const` lets you use the function to compare a `const` `Activity` object with another. – Tony Delroy Nov 06 '14 at 23:56
  • Thanks, it's because I've lots member in the struct, and i do not wand to write a comparison for each member. – Gpouilly Nov 07 '14 at 00:03
  • @Gpouilly you have to write a comparison for each – M.M Nov 07 '14 at 00:46