• Advertisement
Sign in to follow this  

Pure virtual functions or function pointers for C++?

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

Are there advantages to using function pointers over a pure virtual function in a class? I remember some articles stating to stay away from pure virtual functions due to the overhead of calling them. I can’t imagine it being very much but this article had some good information comparing the two. http://www.eventhelix.com/RealtimeMantra/Basics/ComparingCPPAndCPerformance2.htm

Share this post


Link to post
Share on other sites
Advertisement
virtual functions are one of the most central features of C++. If you're not going to use them, you should be using C. Use the virtual functions, that's why they're there.

-me

Share this post


Link to post
Share on other sites
Virtual functions are generally implemented using function pointers, or (in complicated cases) something simpler than function pointers. So it would be very strange for virtual functions to be slower than function pointers, but sometimes they'll be faster. In any case, it's extremely unlikely that there will be a significant performance hit from either one.

Member function pointers in C++ are badly designed and surprisingly complicated under the hood. Unless you are specifically using delegates, don't touch them with a ten foot pole.

Share this post


Link to post
Share on other sites
You likely read that virtual functions are SLOW on the xbox360. Which is true. Otherwise, don't worry about speed until you've profiled.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike2343
You likely read that virtual functions are SLOW on the xbox360. Which is true.

Er... what's your point? Again, virtual functions are implemented using function pointers. The pipeline stall and potential icache miss from virtual function calls will also occur with function pointers. (Besides, which, "slow" is relative; if you're only doing a few thousand virtual calls per frame, the slowdown will be unnoticeable even if you miss the cache each time.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
virtual functions are one of the most central features of C++. If you're not going to use them, you should be using C. Use the virtual functions, that's why they're there.

-me


Templates make for a very good alternative to virtual functions in some circumstances.

But as always, use the right tool for the job. The proverbial shiny new hammer, and all that.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Er... what's your point? Again, virtual functions are implemented using function pointers. The pipeline stall and potential icache miss from virtual function calls will also occur with function pointers.


Yeah, exactly. What's slow about them on the 360 is that they generally incur a cache miss which incurs a load-hit-stall; on the 360 that takes ~500 cycles or so. But you can't get polymorphic functionality without that stall.

All you're saying is that "on the 360, polymorphism is slow no matter how it's implemented". However, it's the correct tool for many instances of logic. Everything in programming is a trade-off; it all just comes down to knowing when a given tool is the correct one.

-me

Share this post


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

  • Advertisement