Man, that is absolutely crazy! Honestly, the more i learn about C++ the more i want to learn about the language, it's like you're fighting a Hydra, you cut one head but two grow as a replacement. I know these things aren't actually easy to get, and since there are like an infinite ammount of things you can do with this language, sometimes it just makes the programmer's life harder. But i think that this just shows that C++ is all about giving control to the programmer to do basically anything he wants to do, which i find it to be fascinating in it's own way. I'm aware that you won't read this comment since this video has been out for a long time, but i seriously am grateful you made this huge ass playlist bro! It's really really helping me to learn this language Thanks bro!
if you mess it up it can be very dangerous too. One of the Syntaxes you have to understand before you try around. Thanks god c++ has smartpointers aswell.
Dude you're so good at explaining stuff. I was trying to understand function pointers for so long and all it took was this video. Also really good job on the threading video. Keep it up!
Another nice use-case of function pointers could be system calls in OS development. You can talk about how the operating system stores an array of function pointer corresponding to each system call and that helps calling functions simply by indexing in that array.
i love that you give a real useful example! ive seen bucky's videos and he never does this. he just tells you the idea and i guess assumes you know when to apply it. great job!
bucky's tutorials are way better. cherno overcomplicates things. there's nothing hes done here that cant be done with just normal functions so he hasn't demonstrated on why u NEED to use function pointers.
Your videos are absolutly amazing! You mix it up! Most youtubers just make you stare at a console through the whole course, and they only use their cursor to explain things. This playlist is well put together and well worth my time. Keep up the good work
I think what makes you so much better than everyone else is the fact that you give examples of where you would actually use all the things you teach in code.
I like your videos, sometimes I just need a quick look into a subject and don't have the time to read the technical reference pages. So I just crank your videos double speed and blast my brain. :D thnx
This brought back memories... I remember back in the early 2000 when I added a console to my game engine and hooked up function pointers to strings to be able to call those functions from the console :P
Function pointers were the precursors to OO. On rare occasion, I still use function pointers ( for dll support) , but really, you should avoid them when they are not necessary, not just because of the obtuse syntax, but because they are too loose - which means intractable bugs. I have seen people essentially reinvent c++ using #define and function pointers. I'm sure they thought it was really clever, but they were the only people willing to maintain the code. As soon as they leave the project, the first thing that happens is others toss their unmaintainable garbage code into the bit bucket and start over.
Your videos are amaizing, I was struggling with pointers and function pointers on my faculty projects and your tutorials made my life easier. Thanks and keep doing the good work.
Function pointers allow us to use the concept of higher order function map, if the map function is too complex, then lambda functions won't be readable. So I think that function pointers are still an important part of C++.
A function and a pointer to the function are different types. When assign a function to a variable, func converts itself to a pointer implicitly, just as you mentioned. On the other hand, we can use a ptr to a func to "interact" with "operator ()" just as func itself, just because the "()" is overloaded implicitly, I guess. Actually, a function is able to be "quoted" by reference with the original type rather than a pointer. But when you want to store several functions to a array to do a batch call, the pointers are stored.
i personally love function pointers. I regulary store them in hashmaps , to call a similar, but different funktion every time. Like when i create elements of some sort, but the functions are only similiar in theory, but the execution is different, even if the result is the same. And at this point, i create a HashMap, calling the functions by keyword, with needed parameters. Thats so cool, it really helps me keep my functions organized, and i can add another element to the Map easily at any point For example when i want to parse Network data from another program, or via Rest APIs
Man have been coding for a while, even that passing function inside another function using function pointer is sort of a overkill but yeah that's a way and can be used. I am majorly from a C background and mostly in my code we have seen the function pointer array but this was next level, the lambda is not known to me much, will go through and understand it.
Great content! I was trying to write a homework and couldn't understand what I was doing wrong. after this video I could fix it immediately. VERY HELPFUL THANK YOU
I learned a similar thing in GameMaker and used for Finite State Machine, where state function is assigned to a variable that is called each game frame.
I've been working with UE4 and dispatchers seem to work based on function pointers, but I was never sure what a function pointer _was_ or how it was _written_ because UE code is so....'nebulous' isn't the right word, it's just kinda hard to track stuff down is all. And this video got me caught up. A+ stuff right here.
I have always struggled with pointers but this video actually turned on a lightbulb for me XD. I know function pointers are different but the "pointer theory" if you will makes much more sense now!
That last bit blew my mind haha. main() calls ForEach which then runs a for loop and "calls" the lambda function which then revisits the for loop can calls the lambda again etc. To make matters worse - ForEach could then still return a value. Lol. Thanks for making my brain hurt Cherno
If it hurts your brain to look at it, then it's usually bad code. I will say, brain hurt is not uncommon when dealing with templates, but it's a necessary pain.
Would be nice to include example with a class member function as well! There’s a couple tricky details to attend to in that case (non static class member function)
I had a tough google session but I managed to figure out how it works. A video would be great tho. There needs to be more on the internet about this subject. 👍
@TheChernoProject I've never seen that kind of for loop! I kinda understand what it does: for (every index in : this array) do this; I'd like to see what other ways that could be used! Could you do a short video, or dedicate part of a video to explaining that. If you've already done that, can you show me where to look? :) Thanks for the vids, they're so helpful
Awesome content. Function pointer are awesome, however I struggle with passing member functions as pointers. So far i have been only able to pass static member functions, if i defiene non static function and try to pass it as a parameter into another function i end up wth this error: non-standard syntax; use '&' to create a pointer to member. Is there a way tou use non static member functions as pointer in c++?
noob question, why do u have '&' after const std::vector is it because you are taking the param as reference and not value? but why if you are not changing the variable? sorry if the questions sounds dumb, I'm just trying to learn