Possible Duplicate:
C++: instantiate class f开发者_如何学运维rom name?
For example, if I have
string s = "myObject";
So how can I use that String s to make this one?
return new myObject();
I tried this one but it's quite silly and it's wrong
return new s();
Thank you
You can create a simple factory and register the classes you want to be able to construct. This is a very light-weight sort-of reflection.
#include <map>
#include <string>
template <class T> void* constructor() { return (void*)new T(); }
struct factory
{
typedef void*(*constructor_t)();
typedef std::map<std::string, constructor_t> map_type;
map_type m_classes;
template <class T>
void register_class(std::string const& n)
{ m_classes.insert(std::make_pair(n, &constructor<T>)); }
void* construct(std::string const& n)
{
map_type::iterator i = m_classes.find(n);
if (i == m_classes.end()) return 0; // or throw or whatever you want
return i->second();
}
};
factory g_factory;
#define REGISTER_CLASS(n) g_factory.register_class<n>(#n)
int main()
{
using namespace std;
REGISTER_CLASS(string);
std::string* s = (std::string*)g_factory.construct("string");
printf("s = '%s'\n", s->c_str());
*s = "foobar";
printf("s = '%s'\n", s->c_str());
return 0;
}
What you want is called virtual constructor (pattern). The availability of this feature in a language is not necessarily coupled to the language being interpreted or managed by a VM - it depends on how (and if at all) the information about types existing in a program (or library) is available at run time. This is not the case in "naked" C++ - but it can be implemented, as shown by Arvid, for example. The problem is that there is no standardized implementation of this feature, so everybody keeps re-inventing this again and again. To certain extent COM or it's platform independent counterpart XPCOM "standardize" this at component level.
You cannot do that in c++, by design. In c++, there is a clear distinction between a type and an object, which are two very different entities of the language. You can thus only instantiate a type and get back an object, and decide to do so at compile time (c++ does not provide any kind of reflection system unlike c# or java).
However, you can use a factory / registry for the objects you want to instantiate that way.
You'll need Reflection to do that. But C++ AFAIK doesn't support reflection.
Interestingly QT C++ Supports some kinda reflection. Look at the C# section for how its done there, and Javascript has one function to do this. Its called eval()
It's not possible to do this with C++ since the code run on it's own. Only interpreted languages, such as PHP, or programs that are run through interpreters allows this kind of feature.
精彩评论