I want to compare C++ class/structure objects. In C, most of the time, one knows exact size of the struct
by adding up the sizes of individual fields (assuming that compiler does not add padding). Hence one can use memcmp() function on two object to compare them very fast. I am not sure if the same works for C++. This is because a class also has function definitions and maybe some other hidden things (some RTTI info perhaps? A virtual function table even?)
A quick program with a simple structure containing int
and char
members and a function showed that size of the structure was sizeof(int)+sizeof(char)
.
I have a one big struct class with simple int, char etc data types (but a large number of them). I want to compare objects from time to time. I cannot overload the ==
operator as that will make them compare each field by field. In C, I can compare in one go using memcmp()
. Any suggestions for C++? Can I use memcmp()
directly? I dont want memcmp() to fail because some other value like virtual function pointer table is different (but all the fields are actually equal)
(I'm using g++)