Sign in to follow this  
leoptimus

Function Pointers VS Virtual function

Recommended Posts

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this