Hi,
Is it possible to find object of my own class?
I need to do something like
#include "myclass.h" //header of my class
//[...]
myclass* a=new myclass("name");
//here I forget about pointer *a
//get back object
myclass* b = (myclass*)gROOT->FindObject("name");
I do not insist on “gROOT->FindObject”. It can be anything what gives me access to created object.
class MyClass {
...
public:
MyClass(const char* name) {fgObjMap[name]=this;}
~MyClass() {fgObjMap[name]=0; } // or remove from map
static FindObject(const char* name) {return fgObjMap[name];}
private:
static std::map<std::string,MyClass*> fgObjMap;
};
(don’t forget to put fgObjMap into your source file, too - it’s a static var). Now you can call MyClass::FindObject(“name”).
[quote]It works well. The only little problem which I can see is that b1->ClassName() return “TNamed” instead of “myclass”. But I can live with it Smile
[/quote]This is the expected behavior if you class ‘myclass’ is interperted (we can not overload the virtual table of a compile object (TNamed) with the one from an interpreted class) or if you did not put ClassDef in a compiled class (because IsA is not properly overloaded).