-4

I want to check if two instances of my class are equal(in C++), there are no pointers in the class. I understand that I would need to override the = operator and manually check all the fields. I am aware of that solution. The problem is that I have like around 100 members in the class, and I would want to do it for many classes of similar sizes.

My questions is if there is a way to templatize this? Is this possible? I am fine using velocity.

quickdraw
  • 247
  • 1
  • 2
  • 12
  • 2
    (You want to override the `==` operator.) How similar ate these classes? You may consider inheritance. And maybe rethink your approach as 100 members are often avoidable (not always). – BlameTheBits May 16 '17 at 06:40
  • 3
    C++ does not support reflection. You would need some preprocessor assisted solution, see e.g. https://stackoverflow.com/questions/19059157/iterate-through-struct-and-class-members – Henri Menke May 16 '17 at 06:41
  • 3
    If the members are all POD, you could use `memcmp`. Otherwise just don't have such huge classes. Perhaps a change in design. – DeiDei May 16 '17 at 06:53
  • Related question: http://stackoverflow.com/questions/217911/why-dont-c-compilers-define-operator-and-operator – chtz May 16 '17 at 07:07
  • 1
    Why is this tagged velocity – Passer By May 16 '17 at 07:11

1 Answers1

-1

You can use a std::tuple. The obvious drawback is that you need to refer to your members by std::get<size_t>().

Ðаn
  • 10,934
  • 11
  • 59
  • 95
chtz
  • 17,329
  • 4
  • 26
  • 56