Sign in to follow this  

get type of object?

This topic is 4203 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 there some way I can get the type of an object and then use it to create a new instance of the object? What i'm attempting to do is to implement a way for my component manager to create components of specified types. In essence, all my components have a string ID and I want to be able to supply this ID to a function that will then return a pointer to a newly create component of the type that is associated to the ID. I can just make a switch-case to see if the ID matches any string and then just hard-code all the creation of these objects. But is there any other, better way to do it? One idea I had was to make the components register themselves in the component manager, but in that case I need to be able to get the type of that object and associate it with it's string ID. Am I making any sense?

Share this post


Link to post
Share on other sites
What language are you writing in?


It sounds like you want an abstract factory. However, it might be worth describing the actual purpose of this part of your design - there's a reasonably good chance you can accomplish the same result with a cleaner and less brittle piece of code.

Share this post


Link to post
Share on other sites
I'm using C++.

What i'm trying to build is a component-based entity system for my game engine. Each entity contains a set of components that build up the actual game logic for that specific entity. Maybe you've heard of this approach before?

EDIT: The abstract factory sounds pretty similar to what i'm looking for. I think i'll read some more about them. Thanks for the tip! :)

[Edited by - DarkZoulz on June 15, 2006 4:13:53 PM]

Share this post


Link to post
Share on other sites
Sorry if i'm not being clear. I'm not that good at explaining things.

What i'm looking for is basicly a means to create instances of my component objects/classes without having to hard-code each one of my components into my createComponent() function.

ex.

IComponent *pResult = NULL;

switch(componentID)
{
case "ACTOR":
pResult = new Component_Actor;
break;

case "NETWORK":
pResult = new Component_Network;
break;
}

return pResult;



That's what my createComponent() function looks like now.

Metaclasses and reflection sounds good too. I'll have to read up on them also.

Share this post


Link to post
Share on other sites
You could store "prototype" instances somewhere, and use a virtual constructor idiom. But the system still needs a way to get at the prototypes.

Effectively, mapping a bit of text data (e.g. from a file) into an object type (and hence an instance of that object type) is a reflection task, whether you do it manually (e.g. building a std::map<std::string, IComponent*> to store your prototypes) or relying on built-in language stuff (C++ doesn't have any in the standard library, but Boost might be able to help you out).

Share this post


Link to post
Share on other sites

This topic is 4203 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