Casting without casting?

Started by
10 comments, last by PureSnowX 11 years, 1 month ago

Casting is not so bad in this case. You have already asked for a TransformComponent so you know what type it is, and components are quite different from each other so you can't do everything from a common interface. I would however make the map private and put the cast into a templated GetComponent function to avoid the possibility of casting to the wrong type. If you do that, it should be safe enough without dynamic_cast.

Copying is not the right thing. It would be horribly inefficient.

The only real alternative to casting would be a message system, where the entity would pass on messages to the components. Maybe save that for the future because it's harder to set up.

Oh, that sounds similar to how unity does it. I will try to see if I can make that to work!(the templated thingy and not the message)

The 2013 Inerbutts Olympics Committee has been notified concerning this thread. An entry is proposed for the category 'longest posts due to needless quoting'. Confirmation of eligibility should be available by the week's end.

Go for the gold, fellas.

But "" is good sad.png

Advertisement

What do you mean by "Those things are bad" ? Advice like that REQUIRES context. It is bad for condition x, but useful under condition y.

To cast to a derived class, use dynamic_cast<>.

But *USE IT PROPERLY*. That means only using it on objects that are the expected type, and being prepared to handle an exception if you use it improperly. If you use it improperly, or if you are in a poor design that forces you to potentially have exceptions, then yes you will have some performance penalties.

Note that what you describe is not generally done, and it usually means you have a bad design.

It is common to downcast (from a derived cast back to the base class) but uncommon to upcast (from the base class to a derived class).

One of the biggest benefits of inheritance hierarchies is that you can create an abstract base class, or interface, and program your systems against that. It provides virtual functions of everything that might change on subclasses. Then you can derive your subclasses and override whatever functionality you need.

If you find that you need to upcast it is a sign that you are likely missing some virtual functions, that your object is likely violating the SRP, that you are likely violating Dependency Inversion principles, and likely making other design mistakes.

With that in mind, what is the REAL problem that you are trying to solve with your 'copy' example?

I'm currently up-casting in my component-system. I have a list of IComponent that I ( with template magic ) static_cast<> to an derived class.
From what I take this is how many does it if they are not using a message-system?

EDIT:
Note on the static_cast<> usage instead of dynamic_cast<>, since I KNOW EXACTLY what component I'll be casting to what class I figured static_cast would be faster.

This topic is closed to new replies.

Advertisement