I’ve been reading a bit about what metaclasses are, but I would like to know if they can be achieved in C++.
I know that Qt library is using MetaObjects, but it uses an extension of C++ to achieve it. I want to know if it is possible directly in C++.
Thanks.
It’s possible to create meta-classes, however C++ is not about that, it’s about statically compile-time based implementations, not runtime flexibility.
Anyways it depends if you want Meta-Classes with methods or just Meta-Classes with data, the Data classes can be implemented with Boost constructs like boost::any, and if you want Classes with methods, you can use boost::bind to bind methods to the object, or you can implement them on your own with a single-entry point interface like COM-objects.
However the ‘real’ C++ way is to use generics so it can all be determined at compile-time, for best performance.
To be honest, I’ve seen very few systems, although I have seen some, that truly need runtime flexibility, in most cases objects are born and die the same class, or at least enough to spend 95% of their lifetime as a single class once they come out of their factory.
So in many situations one finds themselves paying too much for runtime meta-classes. Of course there is the school of thought that this provides better developer performance, but in many situations, each line of code will be run on hardware a few hundred million times more than the time it took to write it. So you can think about compile-time and runtime classes as paying up front or leasing for-ever. Personally I like to pay up front.