Hmm. I'll explain a bit further on what I'm doing and the justification on why I'm doing it.
I'm writing a reflection system which works amazingly well so far, but for different type of Objects (Objects have a specific meaning in this system - it's for all classes that inherit from the class Object) a has to be unique id is given. Objects can only go in a specific type of container. Objects can never be static, or never be in non-pointer form (i.e. created without new).
This is an example of what it can do, and typical use of it;
code
Code: Select all
class Animal : public Object
{
declare_class(Animal)
public:
Animal()
{
std::cout << "Animal Constructor" << std::endl;
x = 1001;
}
~Animal()
{
std::cout << "Animal Destructor" << std::endl;
}
int x;
};
define_class(Animal, t)
{
t.member("x", &Animal::x);
}
void main()
{
Animal* animal = new Animal();
animal->inspect();
delete animal;
}
result
Code: Select all
Animal Constructor
Animal.id=0
Animal.x=1001
Animal Destructor
Anyway, 'id' is an int, stored in Object. I completely realize I could just go through the normal route and set it in Object constructor, but I'd like to use operator new for other things, such as class registration and removal, instead of using 'createAnimal', 'destroyAnimal' methods, or more abstract 'storeObject', 'removeObject' method.
My end goal is to have separate automatic container templated classes for Animal, Plant, Rock, etc. which automatically register and remove instances of these classes. I can't do this in the scope of Object (because it's not a template, and I can't get the Type), and I don't want to do it in the Animal constructor, even if I had a macro. Using the operator new (and operator delete) this way, ensures I can set a unique id, and then register and de-register the class before the constructor or destructor is called.