Runtime type checking help.
-
Greetings! I know it's a long shot and please don't mock me if I missed it completely. Suppose I get some config file called objects.txt. It includes lines like: phonenum|1003 colour|red vehicle|truck|mack first string on the line is name of object type and all the rest are parameters. In my program I would like to read file objects.txt line by line and do the following: if "I know-type" phonenum { new phonenum(1003); } else if "I know-type" colour{ new colour(red); } else if "I know-type" vehicle{ new vehicle(truck, mack); } Is that even possible? To check the type at run-time? Before you dissmiss me complitely I'd like to explain what I would like to achieve. Maybe you will know some other solution to the same problem. I'd like to make some kind of save/load, serialize/deserialize framework. It would provide capability to store and load user defined objects. All objects would be serialized and stored in one file (configuration for the whole user defined system). Framework would gather ojects trough some common interface addObj(abstractObj obj*); every user defined object would have to implement methods char* serialize(void); void deserialize(char*); when framework is asked to save objects, it would call "serialize" method of all objects it contains and save them line by line. When framework is asked to load configuration he would read the file line by line and read the type of object and create it, than call its "deserialize" method with the rest of the line characters. I already used the serialize/deserialize pattern before. But in that particular implementation I knew all the types compile time. For example: if (line == "phonenum"){ new phonenum(1002); } ... here I already know what types I expect. I'd like to do implementation when I don't already know all the names infront at compile time. I'd like to read the "name" run-time and check if I know it. This way I'd really have true framework which should not know any user defined types at compile time. Best regards, Rostfrei
-
Greetings! I know it's a long shot and please don't mock me if I missed it completely. Suppose I get some config file called objects.txt. It includes lines like: phonenum|1003 colour|red vehicle|truck|mack first string on the line is name of object type and all the rest are parameters. In my program I would like to read file objects.txt line by line and do the following: if "I know-type" phonenum { new phonenum(1003); } else if "I know-type" colour{ new colour(red); } else if "I know-type" vehicle{ new vehicle(truck, mack); } Is that even possible? To check the type at run-time? Before you dissmiss me complitely I'd like to explain what I would like to achieve. Maybe you will know some other solution to the same problem. I'd like to make some kind of save/load, serialize/deserialize framework. It would provide capability to store and load user defined objects. All objects would be serialized and stored in one file (configuration for the whole user defined system). Framework would gather ojects trough some common interface addObj(abstractObj obj*); every user defined object would have to implement methods char* serialize(void); void deserialize(char*); when framework is asked to save objects, it would call "serialize" method of all objects it contains and save them line by line. When framework is asked to load configuration he would read the file line by line and read the type of object and create it, than call its "deserialize" method with the rest of the line characters. I already used the serialize/deserialize pattern before. But in that particular implementation I knew all the types compile time. For example: if (line == "phonenum"){ new phonenum(1002); } ... here I already know what types I expect. I'd like to do implementation when I don't already know all the names infront at compile time. I'd like to read the "name" run-time and check if I know it. This way I'd really have true framework which should not know any user defined types at compile time. Best regards, Rostfrei
Will
typeid
help you? Code sample fromMSDN
:class Base {
public:
virtual void vvfunc() {}
};class Derived : public Base {};
using namespace std;
int main()
{
Derived* pd = new Derived;
Base* pb = pd;
Base b;
cout << typeid( b ).name() << endl; //prints "class Base *"
cout << typeid( pb ).name() << endl; //prints "class Base *"
cout << typeid( pb ).name() << endl; //prints "class Derived"
cout << typeid( pd ).name() << endl; //prints "class Derived *"
cout << typeid( pd ).name() << endl; //prints "class Derived"
delete pd;
}
Nibu thomas Software Developer
-
Greetings! I know it's a long shot and please don't mock me if I missed it completely. Suppose I get some config file called objects.txt. It includes lines like: phonenum|1003 colour|red vehicle|truck|mack first string on the line is name of object type and all the rest are parameters. In my program I would like to read file objects.txt line by line and do the following: if "I know-type" phonenum { new phonenum(1003); } else if "I know-type" colour{ new colour(red); } else if "I know-type" vehicle{ new vehicle(truck, mack); } Is that even possible? To check the type at run-time? Before you dissmiss me complitely I'd like to explain what I would like to achieve. Maybe you will know some other solution to the same problem. I'd like to make some kind of save/load, serialize/deserialize framework. It would provide capability to store and load user defined objects. All objects would be serialized and stored in one file (configuration for the whole user defined system). Framework would gather ojects trough some common interface addObj(abstractObj obj*); every user defined object would have to implement methods char* serialize(void); void deserialize(char*); when framework is asked to save objects, it would call "serialize" method of all objects it contains and save them line by line. When framework is asked to load configuration he would read the file line by line and read the type of object and create it, than call its "deserialize" method with the rest of the line characters. I already used the serialize/deserialize pattern before. But in that particular implementation I knew all the types compile time. For example: if (line == "phonenum"){ new phonenum(1002); } ... here I already know what types I expect. I'd like to do implementation when I don't already know all the names infront at compile time. I'd like to read the "name" run-time and check if I know it. This way I'd really have true framework which should not know any user defined types at compile time. Best regards, Rostfrei
-
yes this can be done using RTTI, what framework are you using?
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg
-
Will
typeid
help you? Code sample fromMSDN
:class Base {
public:
virtual void vvfunc() {}
};class Derived : public Base {};
using namespace std;
int main()
{
Derived* pd = new Derived;
Base* pb = pd;
Base b;
cout << typeid( b ).name() << endl; //prints "class Base *"
cout << typeid( pb ).name() << endl; //prints "class Base *"
cout << typeid( pb ).name() << endl; //prints "class Derived"
cout << typeid( pd ).name() << endl; //prints "class Derived *"
cout << typeid( pd ).name() << endl; //prints "class Derived"
delete pd;
}
Nibu thomas Software Developer