Indirectly working with a structure in C++

Indirectly working with a structure in C++

I am trying to create a class that can be given the pointer to a structure. The class has other means to know what goes into the structure, but it does not know the names of the elements within the structure.

By using just the address and knowing the data types can I use pointer math to move through the structure assigning values to the structure via only the address.

You may know of a better way than this.


I’m not sure exactly what you are doing but this should be possible. I would develop some sort of data that describes the structure being passed. For example, you might have a list of values, each one specifying the offset and size of each element.

You could initialize the offset using the offsetof macro, and of course the size using sizeof. If you passed this data to a class, along with the number of fields and the address of the structure, the class should be able to do whatever processing is needed.


Share the Post: