Jump to content
  • Advertisement
Sign in to follow this  
Roots

Quick question regarding performance in dynamic casting

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

BACKGROUND Lets say we have three classes: Parent (an abstract class), and ChildA and ChildB. We want to store all our ChildA and ChildB objects in some STL container, so we make a vector of pointers to Parent objects. Now lets say that ChildA has an Update() function we need to call every time through the main game loop, and ChildB does not. So we'd have to iterate through the vector and do a dynamic cast for every ChildA object and call its update function. (In this example, I'm assuming there is some class member of Parent called type, and ChildA and ChildB objects have a different value for type, so we don't need to try to dynamically cast objects and catch exceptions to find out which pointers are which.) PROPOSAL Instead of doing all these dynamic casts, what if we declare the Update() function as a purely virtual function in Parent, then in ChildB class we just define that function to do nothing? Then when we iterate through the vector, we could check which objects are of type ChildA and only call the function on those objects. QUESTIONS 1) Is doing something like this good coding practice? 2) Is it okay to try to "cut corners" to avoid dynamic casting like this? 3) Should I just not even care about this "optimization" at all? Just curious. [smile]

Share this post


Link to post
Share on other sites
Advertisement
Something similar is mention in the C++ FAQ lite section 20.6.

You're probably better off with the pure virtual function in the parent, and an implementation in ChildB that does nothing. If only because it'll prevent "else-if-heimer's disease", especially if you decided to make a ChildC, ChildD, etc.

Every time you decide to make a new class derived from parent you might have to add an if/else and a check with dynamic_cast. Are you sure you'll remember to do so? Using a pure virtual function in Parent means that any derived class should work without having to change code somewhere else.

Share this post


Link to post
Share on other sites
Q 1) Is doing something like this good coding practice?

Sure. The concept of "updating" that specific child node isn't "invalid" in the sense that it makes no sense with ChildB - it's simply that ChildB is allways up-to-date and thus updating is a null-op.

Q 2) Is it okay to try to "cut corners" to avoid dynamic casting like this?

Cutting corners is often a bad idea, depending on how you define it. But, I'm not of the opinion this is a corner that's being cut.

Think of it this way. If ChildA is allready up-to-date, is it invalid to call Update on it? Usually not, this will just have no effect. The same principle applies to ChildB - it is by definition allready up-to-date, so assuming calling ChildA::Update is valid when it is allready up-to-date, then callinb ChildB::Update should be similarly valid when it is allready up-to-date (as it allways is by definition) even though it has no effect.

Q 3) Should I just not even care about this "optimization" at all?

"It depends". For the sake of optimization alone? Yes, if it's having a noticable effect on your program's preformance. Finding out if it is requires the use of a profiler. If it isn't (this includes if the program dosn't run yet), I would worry first about proper design - as a proper design eases modification, be it for future preformance concerns, new features, or just plain mantinence.

transfer( my.wallet , world , usd( 0.02 ) );

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!