A Class that Creates Classes...?

Is it possible to write a class that will create classes? Broad question I know...
Last edited on
It's possible to create a class that creates other objects, but because classes are not first-class objects in C++ you can't create them on runtime.
Last edited on
what type of objects can a class create? how difficult would this process be? would another programming language be more effective for this type of action?
Last edited on
Before anything else, a "class" is to be regarded as a "building plan" that describes a type of objects. So, in most cases a class would provide a constructor to create objects described by the class. Other than you can also create objects within static or non-static methods of a class and return those.

I am not really sure what you are actually asking though, it would be best if you'd be a little bit more specific.
Not possible in C++. You can only create objects from classes. Classes are completely defined at compile time.

Some languages (e.g. smalltalk) allow adding and removing variables and methods from classes at runtime so you could build up
a new class that way.

(I'm assuming you want to create new classes at runtime?)
yes, I was considering building new classes at runtime...its just theory I'm interested in though. I will check out smalltalk...
You can create a base class named 'type', composed of a container of element's of type 'type*' (if the class has attributes), and a container of elements of type 'method*', a type that could take in input a container of arguments of type "type " and return an element of type 'type'. Then overload 'type' and 'method' and build the correct type and method childs in the constructor of 'type' childs.

I hope i was clear. Of course, the extra stuff done at runtime that is done at compile time for normal types will bring performance losses. I use something like that in a project so it works but is a lot less flexible than a normal class you define

Template could be more adequate for this, but i don't know them so i can't advise on the subject
Can you explain a little more about what you are trying to do and why you need to build new classes?
closed account (3hM2Nwbp)
An interesting read: (Meta-Object-Protocol)

http://www.vollmann.ch/pubs/meta/meta/meta.html

I think that I'll personally wait for the year 3029 C++ standard that includes reflection.
Last edited on
Topic archived. No new replies allowed.