VC++ equivalent of eval
-
Hi, In javascript there is an Eval method to do evaluation of string. Can i do something like this in VC++, MFC. Basically i want to create an oblect of the class. The name of the class is dynamic. Example: Eval("new MyClass") // Something like this. Any help would be highly appreciated. Kuldeep
-
Hi, In javascript there is an Eval method to do evaluation of string. Can i do something like this in VC++, MFC. Basically i want to create an oblect of the class. The name of the class is dynamic. Example: Eval("new MyClass") // Something like this. Any help would be highly appreciated. Kuldeep
Bad times on the horizon. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Hi, In javascript there is an Eval method to do evaluation of string. Can i do something like this in VC++, MFC. Basically i want to create an oblect of the class. The name of the class is dynamic. Example: Eval("new MyClass") // Something like this. Any help would be highly appreciated. Kuldeep
No you can't do that. C++ is not a scripting language. You could integrate a scripting language (like Python I think) but it may be overkill for what you are trying to do. What do you need to do exactly ? If you can narrow it down, maybe there are some 'simple' solutions for your problem. You'll need to explain your problem a bit more in details.
Cédric Moonen Software developer
Charting control [v1.3] -
Hi, In javascript there is an Eval method to do evaluation of string. Can i do something like this in VC++, MFC. Basically i want to create an oblect of the class. The name of the class is dynamic. Example: Eval("new MyClass") // Something like this. Any help would be highly appreciated. Kuldeep
Kuldeep Bhatnagar wrote:
The name of the class is dynamic.
Compare name of given class and instantiate it!
Kuldeep Bhatnagar wrote:
Eval("new MyClass") // Something like this. Any help would be highly appreciated.
Have a look at RUNTIME_CLASS macro in MFC, though not similar to Eval!
Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
-
No you can't do that. C++ is not a scripting language. You could integrate a scripting language (like Python I think) but it may be overkill for what you are trying to do. What do you need to do exactly ? If you can narrow it down, maybe there are some 'simple' solutions for your problem. You'll need to explain your problem a bit more in details.
Cédric Moonen Software developer
Charting control [v1.3]Basically i want to make an object of the class and the name of class will be decided at runtime.
-
Basically i want to make an object of the class and the name of class will be decided at runtime.
There are several 'ways' to do that. One common things is that all those classes inherits from one common base class (because anyway you need a way to 'store' those items after they have been created). The simplest one is to make a big 'switch' (not a real switch but replace it with if/else clauses because switch only works with integral types not strings). That's an easy solution but not very flexible. Otherwise you can google for the factory design pattern. That's what it does.
Cédric Moonen Software developer
Charting control [v1.3] -
There are several 'ways' to do that. One common things is that all those classes inherits from one common base class (because anyway you need a way to 'store' those items after they have been created). The simplest one is to make a big 'switch' (not a real switch but replace it with if/else clauses because switch only works with integral types not strings). That's an easy solution but not very flexible. Otherwise you can google for the factory design pattern. That's what it does.
Cédric Moonen Software developer
Charting control [v1.3]Switching was always in my mind. Biut i wanted to do without that.
-
Hi, In javascript there is an Eval method to do evaluation of string. Can i do something like this in VC++, MFC. Basically i want to create an oblect of the class. The name of the class is dynamic. Example: Eval("new MyClass") // Something like this. Any help would be highly appreciated. Kuldeep
This article may be of help - I know it was for me. Enumerate your leaf classes[^] You can have classes registering a name, that you can then use to create them. There's functions to get the list of names, so you could add them to a list control if you liked. From his article:
IMPLEMENT_LEAF_CLASS(CBlueFilter, CBaseFilter, _T("Blue Filter"))
Good luck, Iain.
Iain Clarke appears because CPallini still cares.