While doing my labs, I need to write or modify similar codes over and over again (such as sorting objects or modifying their attributes’ values).
My problem is: Is there a solution to input the name of the member so that I can simply paste it it when I need it to process different classes or structs?
Luckily, I found two solutions. This one calls the function offsetof()
to get the offset of the member. It works well when I use struct but shows warning when I use class. This one uses template but you have to know the name of member before compilation.
Here I put the code below:
offsetof()
solution:
1 | struct Test |
template solution:
1 | template <typename ObjType, typename MembType> |