Jump to content
  • Advertisement
Sign in to follow this  
leoptimus

Function Pointers VS Virtual function

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

For support polymorphism In c++, What is faster? Using abstract classes with virtual functions? or using a look-up table with function pointer?

Share this post


Link to post
Share on other sites
Advertisement
Considering virtual functions are usually implemented by the compiler with a function pointer look-up table, I'd say any benefits you might get by rolling your own are negligable. Just use the virtual keyword and save yourself some time :P

Share this post


Link to post
Share on other sites
They're the same thing under the hood. C++ does the hard work for you with virtual functions though, so it's worth going that route if your program suits it. And you may possibly find that virtual functions are faster due to the thiscall calling convention. But really this is a question for 1996, not 2006, unless you're running heavy duty scientific data processing, or porting Doom 3 to the 8080 chip.

Share this post


Link to post
Share on other sites
If you want a basic comparison, look at the generated asm. For simple function call, it looks like:
  mov ecx, address_of_the_target_object
push_function_arguments
call function
..
function:
function_code
For virtual functions, it look like:

  mov ecx, address_of_the_target_object
push_function_arguments
call function
..
function:
mov eax, [ecx+offset_in_vtable]
jmp [eax]
So far, you'll only save a mov and a jmp, at the expense of doing all the error-prone, difficult to debug, hard work by yourself.

I'd be you I'd go the virtual functions way [smile]

Share this post


Link to post
Share on other sites
Yet you don't save those operations if you still have to look up a function pointer in a table to be able to use it.

Share this post


Link to post
Share on other sites
Think of it this way:
If it were ever faster to ignore the built-in language constructs and use your own "look-up table with function pointer[s]" then don't you think they'd change the way that the language constructs were implemented to do the same thing?![smile]

Just use the language as intended.

Share this post


Link to post
Share on other sites
If you have to ask, use the language as built.

Even if you don't, you should normally use the language as built.

If you're one of those exceptionally few people who could ever actually gain a benefit from reimplementing a core language feature, you should be looking for a job writing the compiler and/or writing papers on your uber-l33t optimization, rather than whatever two-bit project it is you're on now.

In short - abstract classes. You should only ever mess around with function pointers when abstract classes *can't* do the job (sometimes this is because you're building an interpreter or in-game console; sometimes it's because you need to pass raw data to someone else and you have to store your function pointers outside the actual object - and in this case, you probably have either deeper design issues somewhere else, or a really crappy API).

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!