Sign in to follow this  

Creating a class object on run time in C++

This topic is 4246 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Is it possible to use RTTI to create a class object at runtime - like for example do something like Class obj = something("CLASSNAME").getClass(); ? I know I can do it by using a function which returns the class based on the string but does a thing already exist like something like typeid(name).getClass(); ?? Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by CRACK123
I know I can do it by using a function which returns the class based on the string
No, you can't. In C++ classes are not objects that you could return from a function.

Share this post


Link to post
Share on other sites
If you're using the .NET framework, you can do this using code emission within the reflection namespace.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconemittingdynamicassemblies.asp

However, I'm not sure if you can do this in managed C++.

Also, look up "template metaprogramming."

Share this post


Link to post
Share on other sites
Hi

Can you explain how they do it - I know one way you could do it is by doing the following(inherit a base class everytime ) and use a function which goes something like the following


Obj *GetClass(const std::string &className)
{
if(className.compare(someName))
{
return new ClassName();
}

return NULL;
}



Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by CRACK123
Hi

Can you explain how they do it - I know one way you could do it is by doing the following(inherit a base class everytime ) and use a function which goes something like the following

*** Source Snippet Removed ***
That function returns an instance (=object), not the class itself. Thus it should be named differently.

Share this post


Link to post
Share on other sites

This topic is 4246 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this