Jump to content
  • Advertisement
Sign in to follow this  
BradDaBug

Getting object types

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

Let's say I've got some sort of parent class with several child classes that inherit from the parent. Now to perform a certain task I need to know the type of child class I've got. I suppose one way to do it would be like this:
// 'parent' is a pointer to a CParentClass which is actually
// an instance of the child classes

CChildClass1 * child1 = dynamic_cast<CChildClass1*>(parent);

if (child)
{
    // do stuff
    return;
}

ChildClass2 * child2 = dynamic_cast<CChildClass2*>(parent);

if (child2)
{
    // do stuff
    return;
}
But this way is pretty lame. Or I could implement some kind of GetType() method, like this:
CParentClass::Type type = parent->GetType();

if (type == CParentClass::ChildClass1)
{
    // do stuff
}
else if (type == CParentClass::ChildClass2)
{
    // do stuff
}
But this way would require me to add a new value to CParentClass::Type each time I created a new child class type. Surely this is a really common problem. What's the best way to handle it?

Share this post


Link to post
Share on other sites
Advertisement
Quote:

What's the best way to handle it?


Alternate design.

Anything like that which is a parameter should be a data member of the base class, not an inherited class.

Share this post


Link to post
Share on other sites
"Now to perform a certain task I need to know the type of child class I've got."

No you dont. If you do, its a design flaw. Don't do this.


If you can't just use the virtual functions the same way, no matter what the derived class ultimately is- its a design problem. Perhaps the derived classes shouldnt be grouped in under the same parent class?

Share this post


Link to post
Share on other sites
Well, unless Ia mmissing something, one way to do this is a virtual function. Each class overrides the function as always to give it the functionality needed in each classe's case. Is there a reason this simple solution won;t do the job in this case?

Share this post


Link to post
Share on other sites
In my program the "// do stuff" is actually code to send messages across a network. I've got an array of CParentClass pointers that point to various types of children and I want to send the appropriate message to the client so that it can create the proper type of child. To use virtual functions I'd have to make every child class aware of the networking system, right? I don't really want to do that.

Share this post


Link to post
Share on other sites
Quote:
Original post by BradDaBug
I'm not sure what you mean. What should be a data member of the base class?


The 'type', or otherwise whatother information defines the children classes as different.

For example, if the base class is "unit" and the children are 'infantry' and 'tank', then there should only really be one class. The unit class, which has some data member to specify if the particular instance is an infantryman or a tank.

Share this post


Link to post
Share on other sites
But the child classes are so completely different that there's not really any way I could combine them into a single class.

Lemme start over. I've got a CObject class, and from that class are inherited classes like CProp (for dynamic objects), CDooDad (for static objects), etc. There's no way these classes can be combined. All these classes are sitting in an array on the server, and I want to send messages to the client so that the client can create its own copy of all these objects. So in pseudocode, this is what I'm trying to do:

for each object in array
if object is a CProp
msg = "create new CProp with CProp properties blah blah blah"
else if object is a CDooDad
msg = "create new CDooDad with CDooDad properties blah blah blah"
else if ...

All the properties for these child classes are going to be completely different.

Share this post


Link to post
Share on other sites
You will need to have a virtual function that requests a creation or hash string (whatever you perfer to call it) from the child. The string will contain all the data needed to recreate that instance.

Its definately best to use a virtual funciton here to seperate all theese functions since as you say, each dirved is radically different. It will keep your parent much cleaner.

Also, the child doesnt need to know anything about the networking system. Just how to create itself from a string, and to collapse itself into one.

The base class can then use this virtual method to get the string, and then do all the networking stuff. Each child doesnt need to know the networking system at all. It only needs to know how to hash itself. The base can take care of the rest.

You can use the boost::serialization library to easily collapse and create from a string. Instead of sending the string to a file stream thou, you can send it to a stringstream so you can use right away.

Anyway, best of luck.

p.s Why are grouping radically different objects under the same base? Thats a no-no (thou i admit that its sometimes quite useful, its a maintence nightmare)

Share this post


Link to post
Share on other sites
I guess that's one option, but I should have mentioned that the actual message passing system in my program is more complicated than just building a string.

And wouldn't doing it that way still leave me with the same problem? I'd have to insert something into the string so that whatever marshalls the de-serializing knows what type of class needs to do the de-serializing, right? And then each class type would still have to be able to provide a unique ID?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!