Creating additional fields in an object during runtime?

Hey,

I'm currently trying to dynamically change the layout of an object. What I try to have is a basic layout which applies to all objects of this class. But in some cases my program decides that an object needs an additional variable. The main drive behind this is memory savings, so I do not want every object to have this additional variable but only some of them. I've been thinking about two possible scenarios but I don't know if they would work as I've never used them before:
1.: Create two different classes and make them compatible by overloading the typecast operator. This should work in both directions (I think) as in one case I would need to instantiate the new variable and in the other direction I would just disregard this variable.
2.: Usage of templates. I would have the additional variable as either my variable type or as void (what's the size of void?). Would it be also possible to not even create this field using templates (to save even more space)?

Thanks alot in advance!
It seems you are looking for inheritance.
Topic archived. No new replies allowed.