D. Susman said:
Since this looks like hecking C code inside C++, this isn't quite
safe, eh?
I believe you are using some strange definition of the word "safe".
There is nothing inherently *dangerous* about 'memcmp' or virtual
functions, provided you're not in the UB land.
It is possible that you're trying to save yourself some typing when
you are looking to delegate the equality comparison to the base class
like that. It is better to implement the operator== for each derived
class and let it compare the data itself.
Allowing 'memcmp' to do the work and hoping for the correct result is
rather /irresponsible/ of the implementor. You want to avoid having
to edit the class' op== function when adding another member, is that
it? What if you add a member of the type 'std::string'? Comparison
of strings is quite well-defined and it does not actually involve
comparing pointers in the bitwise manner. Same for any other
standard containers.
Once a month (maybe once a quarter) we see a question "how do I make
some function do some operation for all members of the class?" There
is no way currently in the language to do that. And I believe the
creators of the language did not want to add this because it only
makes sence in a very small number of cases.
Bite the bullet, make every derived class implement its operator ==
that does the correct thing when comparing two objects. Put the
appropriate semantics there. And document the necessity to revisit
the implementation of that function when adding new members. No
proper design/implementation/maintenance technique can be easily
substituted with some magical language trick.
V