I have an assignment that asks to write a function for any data type. The function is supposed to print structure bytes and identify the total number of bytes that the data structure uses, and differentiate the bytes used for members and bytes used for padding.
My immediate reaction, along with most class reactions, was to use templates. This allows you to write a function once and collect the runtime type of the objects passed to the function. Using memset and typeid, you can easily accomplish what was set. However, our prof. just saw our discussion of patterns and damned patterns in hell.
Seeing this, I was thrown for a cycle, and I'm looking for a little guidance as the best way to get around this. Some things that I looked at:
- explicit cast void pointers (it looks like it will be randomly)
- a base class with virtual functions, from which all data structures are inherited, seems a little strange.
- a base class with "friendship" for each of our data structures.
- rewriting a function for each data structure in our set of problems (I think this is the worst possible solution).
I was hoping I missed the general C ++ tool, does anyone have any ideas?
source
share