Introduction to Function Pointers in C++: What are they and how are they used?

preview_player
Показать описание


This video is an introduction to function pointers as used in C++ and C. We look at the basic syntax, and then run through examples of where function pointers are commonly used.

For a list of sources and inspiration for the code and information presented in this video, please see the end of this video description!

Software used to make this vid:

Further reading examples and inspiration for this video:
Microsoft Windows Forms programming guides:

Function Pointers in C and C++ by Alex Allain:

Geeks for Geeks:

The Cherno YouTube channel video on Function Pointers:

Wikipedia:

Stack Overflow – Many, many excellent questions and answers on this one!

The sources and inspiration for this video are numerous. I have tried to include all the sources I read in preparation for this video, but if you believe I have used an example from your text without appropriate attribution, please contact me on FaceBook or YouTube :)
Рекомендации по теме
Комментарии
Автор

Awesome video, the only concern I have with it are the colours choices, those highlights in blue with a dark background don't really pop out, but the content is really good

fgico
Автор

In C you could create a struct with one so you could specify a function for an object (like a C++ class function). In my Deluxe Pacman game the library I used for the game has a sort function where you provide a function for it to use for your sorting. I used it to sort dirty rectangles (areas of the screen that changed and needed updating rather than page flipping etc, MUCH MUCH faster, it's too bad it isn't used much anymore). So my game only redraws areas that actually change each frame.
Anyhow, great video. You explained these really nicely!
Been working on procedural roguelike dungeon generation lately.

NeilRoy
Автор

You're like a code angel, delivering the salvation of code that I didn't know I needed, exactly when I needed it most.

PrintEngineering
Автор

FANTASTIC video. I like the more well known concepts that you cover since I always learn a bit of information I never did. Awesome stuff!

deudeskrub
Автор

Function pointers in their own are fine as you'll find them being used a lot in the background and they allow for concepts like the strategy pattern. It's just the level of abstraction that matters. If you expose them in application level as an mutable reference value, it's obvious you will run into issues. Even Vtables and lambdas are based on fn ptrs. Lambdas are pointing to a member function that can be invoked with the capture as context. It wasn't mentioned, but there's also a small difference in mem fn ptrs and ordinary fn ptrs, especially the way they are being invoked (unless working with std::invoke).
I've seen some devs using fn ptrs to JIT compile functions into the application in order to escape long compile times or optimize certain parts.
As another interesting topic, I'll suggest coroutines. Only a small proportion of people know how these actually work. It would be interesting to show how these state machines are implemented on assembly level.

bobelche
Автор

I learned more in depth about C++ from your channel than I did from anywhere else.
Thank you so much for your time and efforts!

salmansahel
Автор

Even though I always think you're probably a bit obnoxious when I start the video, it always turns out not to be the case at all. You explain every concept clearly and without "bloating" the video length and I very much enjoy the diversity of your content.

smiley_
Автор

C# equivilent of a function pointer once proved extremely useful for me in a college project
We were supposed to write a program on the console with a menu, since it was(still is by the time of this message) the first year everyone was using switch case
I was too lazy to write 7 cases so I implemented a dictionary of delegates(function pointers) with the console cmd as the key and the delegate as the value so it was automatically called based upon the user input.
It was so much fun.

sagivalia
Автор

Nice video, very informative. You have produced what may be the most intelligent and easy to follow programming tutorial channel on you tube. A few years ago, while tinkering with pointers, I concluded that a function pointer was the memory address of the first instruction of the function. The disassembly that you showed us confirmed my previous conclusion: thanks.

WrinkledHound
Автор

Amazing, wish there will be more contents about C++, and DirectX, CUDA or other similar stuff, you make all the concepts really easy to follow.

onigumo
Автор

This is a pretty powerful technique and ability. It's extremely useful in game hacking, function pointers are used to call internal game functions if you know their address in process and their prototype. Basically you can invoke any function in the given process for whatever you want to achieve, and that opens doors for many things to be done.

Apollyion
Автор

This is super cool!! I'm a CS student and they started us on C++, and I really took a liking to it. Lately I haven't been doing anything in it, and it just feels good to learn something cool like this in a language that I like. Thanks :)

chloem.
Автор

Really enjoying this content. Upbeat, clear communication, great organization, great visualizations. Cheers fella, appreciated.

Fujitechs
Автор

O my.... Super!, Thank You, Your examples explain me clearly why function pointers are needed :) I read a lot about that problem, but not everybody could explain that with easy examples why they are useful, I was thinking that this is old technique from 90' where C was only used . I was thinking that function pointers have no sense where I can cal functions directly. But now I see I can add functions in to arrays and group them for easier use. :D

maximo
Автор

Function Pointers: wormholes in disguise that supposedly kinda give you behavior "polymorphism" that can't be easily optimized/inlined by the compiler

SimGunther
Автор

Very Nice dude! Really liked the tutorial, GREAT Guide you made today!

TheMR-
Автор

Dude I enjoy and learn a lot from your videos. I watched your branchless video and started applying that to my work, thank you. Keep doing you and making videos.

soliderarmatang
Автор

ooh i just found your channel and im early to a video nice :D can't wait to see yiour amazing content

lythd
Автор

Another great video! Also, that weird syntax thing was very sensible when I tried to look at assembly and tried to think how compiler will know what I am doing, thought to emphasize that for anyone finding it weird

cortexauth
Автор

Chris you the man these things have boggled my mind for a long time, especially the syntax, impressive looking on screen but just naff. Thanks pal

steveokinevo