The Benefits of Using Function Pointers

2019-01-31 09:26发布

I have been programming for a few years now and have used function pointers in certain cases. What I would like to know is when is it appropriate or not to use them for performance reasons and I mean in the context of games, not business software.

Function pointers are fast, John Carmack used them to the extent of abuse in the Quake and Doom source code and because he is a genius :)

I would like to use function pointers more but I want to use them where they are most appropriate.

These days what are the best and most practical uses of function pointers in modern c-style languages such as C, C++, C# and Java, etc?

11条回答
Juvenile、少年°
2楼-- · 2019-01-31 10:05

These days what are the best and most practical uses of integers in modern c-style languages?

查看更多
一夜七次
3楼-- · 2019-01-31 10:05

Function pointers are used as callbacks in many cases. One use is as a comparison function in sorting algorithms. So if you are trying to compare customized objects, you can provide a function pointer to the comparison function that knows how to handle that data.

That said, I'll provide a quote I got from a former professor of mine:

Treat a new C++ feature like you would treat a loaded automatic weapon in a crowded room: never use it just because it looks nifty. Wait until you understand the consequences, don't get cute, write what you know, and know what you write.

查看更多
三岁会撩人
4楼-- · 2019-01-31 10:08

Function pointers are fast

In what context? Compared to?

It sounds like you just want to use function pointers for the sake of using them. That would be bad.

A pointer to a function is normally used as a callback or event handler.

查看更多
冷血范
5楼-- · 2019-01-31 10:09

Any time you use a event handler or delegate in C#, you are effectively using a function pointer.

And no, they are not about speed. Function pointers are about convenience.

Jonathan

查看更多
The star\"
6楼-- · 2019-01-31 10:10

Just speaking of C#, but function pointers are used all over C#. Delegates and Events (and Lambdas, etc) are all function pointers under the hood, so nearly any C# project is going to be riddled with function pointers. Basically every event handler, near every LINQ query, etc - will be using function pointers.

查看更多
我想做一个坏孩纸
7楼-- · 2019-01-31 10:14

In the dim, dark ages before C++, there was a common pattern I used in my code which was to define a struct with a set of function pointers that (typically) operated on that struct in some way and provided particular behaviors for it. In C++ terms, I was just building a vtable. The difference was that I could side-effect the struct at runtime to change behaviors of individual objects on the fly as needed. This offers a much richer model of inheritance at the cost of stability and ease of debugging. The greatest cost, however, was that there was exactly one person who could write this code effectively: me.

I used this heavily in a UI framework that let me change the way objects got painted, who was the target of commands, and so on, on the fly - something that very few UIs offered.

Having this process formalized in OO languages is better in every meaningful way.

查看更多
登录 后发表回答