In Java I can instantiate an object using the 'Class' method 'newInstance' which for my particular system I find very helpful indeed. I am now trying to achieve something similar in C++.
It has not been obvious to me how this might be achieved but after some reflection .... (do you get it ... huh?) I think that it might be possible by creating a ClassDescription class which has an attribute holding the 'sizeof' the class instance and a method newInstance which mallocs this amount of memory and returns it as a void *. The calling code will then need to cast this appropriately.
Does the C++ language suitably define whether this is valid?
By the way .. I recognise that I could create a registry holding Factories for the classes which is a backup plan. For answers to this question I would value focusing on the specific question of whether what I have discussed will work.
Best Regards
* Additional Context * The reason for this requirement is to allow a generic library to instantiate classes which the library user is aware of but not the library itself. The library will have some meta data to use to achieve this and so could be told the sizeof the class. It is 'neater' from the user perspective not to have to add a factory object to the the meta data.