|
-
June 19th, 2009, 12:29 AM
#1
class from string
Hey,
So let's not get into the potential ridiculousness of what I'm doing. But essentially I have a string that is the name of a class, I want it to create a new object of that type.
So if I pass it a string "ClassyFace", it'll create a new ClassyFace object, if the string is "ClassyPants", I want it to make a ClassyPants object. I realise I can achieve this with a bunch of if statements but I'd like to have it go directly from the string. Also, if it helps, all of these objects share a base class.
Any help is appreciated.
-
June 19th, 2009, 12:49 AM
#2
Re: class from string
Personally, I would just go with an if else chain. C++ does not have a reflection feature, at least not natively.
-
June 19th, 2009, 01:33 AM
#3
Re: class from string
Yes, make a factory class/method that creates the objects base on the name of the class given as string.
-
August 5th, 2009, 06:45 PM
#4
Re: class from string
Yeah, that's what I thought. Silly C++. Not being java :P
-
August 5th, 2009, 07:54 PM
#5
Re: class from string
Yeah, that's what I thought. Silly C++. Not being java :P
Are you hear to publish points of disagreement? If you know and like Java, why are you trying your hand at C++?
French isn't silly because it's not German, or English. C++ wasn't created to satisfy the interests of Java developers, it usually serves rather different purposes, though there can be considerable overlap.
There's a book from Alexandrescu on the subject of metaprogramming (typelists specifically in this case) which can be used to create a simple template function which could do what you ask.
Last edited by JVene; August 5th, 2009 at 07:57 PM.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|