Function Pointers in C++

Поделиться
HTML-код
  • Опубликовано: 23 ноя 2024

Комментарии • 355

  • @jun7452
    @jun7452 7 месяцев назад +29

    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!

  • @kinggore5229
    @kinggore5229 4 года назад +105

    Someone give this man a medal, pls.

  • @PythonPlusPlus
    @PythonPlusPlus 6 лет назад +582

    Who else is binge watching the playlist?

  • @vipulkamani6538
    @vipulkamani6538 5 лет назад +166

    By far the best videos on C++. Thanks man! You just taught me overnight what my professor couldn't in 7 weeks.

  • @neo-babylon7872
    @neo-babylon7872 5 лет назад +64

    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!

  • @shah.kairav
    @shah.kairav 3 года назад +13

    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.

  • @BlackJar72
    @BlackJar72 6 лет назад +212

    A+++++ content! I've been wanting to learn more about function pointers.

  • @tyreerobinson2016
    @tyreerobinson2016 4 года назад +15

    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

  • @ryanbinter2447
    @ryanbinter2447 6 лет назад +19

    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!

    • @reenamola2162
      @reenamola2162 2 года назад

      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.

    • @TheCodeHunter
      @TheCodeHunter 2 года назад +1

      @@reenamola2162 cherno goes into proper detail, Bucky just scratches the surface

  • @eventhisidistaken
    @eventhisidistaken 4 года назад +18

    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.

  • @samsturdi
    @samsturdi 5 лет назад +23

    Hey dude, just wanted to let you know you are my go to for getting foundations of principles. You've helped me out several times. Thanks so much!

    • @mtraven23
      @mtraven23 6 месяцев назад

      5 years later, his videos are doing the same for me.

  • @alik250
    @alik250 6 лет назад +3

    This is the greatest coding tutorial series iIve ever seen. If only somebody as talented and knowledgeable about the subject would make similar styled tutorials for other coding languages, the world would be a much better place

  • @miles7267
    @miles7267 4 года назад +1

    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.

  • @vvanchesa
    @vvanchesa Год назад +1

    Found your c++ series now. To learn C++ and dive in quickly your videos are absolute great!

  • @DanielLiljeberg
    @DanielLiljeberg Год назад

    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

  • @iteratedofficial
    @iteratedofficial 3 года назад

    Probably the best c++ tutor on youtube

  • @SaifuddinEvloev
    @SaifuddinEvloev Год назад +2

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

  • @AswinJP
    @AswinJP 4 года назад +5

    Very few people do such an excellent job at this. Thank you Cherno! :)

  • @ETZERBEATS
    @ETZERBEATS 3 года назад +1

    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.

  • @yang-z
    @yang-z 4 года назад +1

    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.

  • @NeoKailthas
    @NeoKailthas 3 года назад +13

    This is like when you are half way through the semester and half the class dropped out or changed major.

    • @some_one
      @some_one 3 года назад

      i just want to see how many are left in the end

  • @annahakobian7371
    @annahakobian7371 5 лет назад +3

    best c++ tutorials, thanks man

  • @hlrbBrambleX
    @hlrbBrambleX 3 года назад

    I just realized this is how callback function works. Amazing!

  • @ectoid6316
    @ectoid6316 5 лет назад +7

    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

  • @muhammadarslanrana942
    @muhammadarslanrana942 4 года назад +3

    with this we can implement map(), reduce(), filter() functions as well.

  • @josuevargas7252
    @josuevargas7252 6 лет назад

    Jesssss.......How incredible channel full of C++ special content.Thankyou very much.It's hard to find something like you here in my classroom....

  • @Hasansaid51
    @Hasansaid51 4 года назад

    That explanation at 1:04 just clicked in my head and I didn’t even watch the rest of the video. Thanks brotha 🙏🏾

  • @robfei-u6b
    @robfei-u6b Год назад

    this lesson which involves some many technical things such us auto, vector, typedef, function pointer, reference, even iterator inside

  • @Ebiko
    @Ebiko 2 года назад

    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

  • @GenericPhantom1
    @GenericPhantom1 Год назад

    The name says it all. It points towards a function instead of a variable.

  • @Fol2ey
    @Fol2ey 4 года назад

    Dude. Your explanations are saving my life right now!

  • @qy4247
    @qy4247 3 года назад

    Really good tutorial. Thx from China ❤

  • @duchaolv5876
    @duchaolv5876 3 года назад

    Every time I want to know something about modern c++, I come here. Good job Cherno!

  • @NeZversSounds
    @NeZversSounds 4 года назад +2

    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.

  • @DiogoManteu
    @DiogoManteu Год назад

    Such a great explanation with examples.

  • @onyinyechichukwuma689
    @onyinyechichukwuma689 3 года назад

    The best C++ explanations EVER!
    Thanks!

  • @diegomora997
    @diegomora997 2 года назад +1

    Dude, you make me feel more exited about computer science

  • @sunilpatro1047
    @sunilpatro1047 5 лет назад

    Awesome explanation. I did search a lot but couldn't understand. But now all my doubts are clear after watching this video. Great content.

  • @charr6108
    @charr6108 2 года назад

    OMG!
    did i just learnt lamda and function pointer in 10 minutes... awesome!

  • @PirKate
    @PirKate 3 года назад

    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

  • @nandukrishna8142
    @nandukrishna8142 2 года назад

    That helped me so much in writing my own game engine with glut window and opengl

  • @figurehe4d
    @figurehe4d 6 лет назад

    excellent video. well explained. function pointers seem useful but the way I've been exposed to them so far only highlights the confusion!

  • @abhisheksa6635
    @abhisheksa6635 Год назад

    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.

  • @AngelTaylorgang809
    @AngelTaylorgang809 4 года назад +3

    Start a data structure series please, greetings from Texas

  • @jakobhuetter
    @jakobhuetter 3 года назад +1

    Love your videos! Helping me a lot with my university stuff!

  • @andrellmarrow4642
    @andrellmarrow4642 9 дней назад

    super informative. thanks man

  • @NeoKailthas
    @NeoKailthas 3 года назад +5

    "Gentle introduction" lol 😆

  • @Cyberfoxxy
    @Cyberfoxxy 5 лет назад +8

    GUI benefits greatly from event driven patterns.
    void(*onclick)(int x, int y);
    If nobody uses this syntax, what is my alternative?

    • @APaleDot
      @APaleDot 5 лет назад +17

      You should probably just use typedefs. So, if you had, let's say, a button class with an onclick event:
      typedef void(*ClickEvent)(int x, int y);
      class Button {
      public:
      ClickEvent onclick;
      }
      Then you could set each individual button's onclick by setting the onclick member directly:
      someButton.onclick = [ ] (int x, int y) { //some code here };
      or:
      void clickHandler(int x, int y) { //some code here }
      someButton.onclick = &clickHandler;

  • @nuriya9000
    @nuriya9000 5 лет назад

    Thank you so much!! I was revising cpp and I got stuck here. This really helped me a bunch. Definitely subscribing!

  • @hishamsomroz6913
    @hishamsomroz6913 3 года назад

    finally a channel whose videos i dont have to watch at 1.5x or 2x

  • @leducphuclong
    @leducphuclong 7 месяцев назад

    Thank you so much !! Great Teacher !!

  • @santhoshnagarajs3489
    @santhoshnagarajs3489 6 лет назад

    finally i understand why there is need for function pointers!!
    thank you .

  • @momentswithjireh3921
    @momentswithjireh3921 3 года назад

    Thank you Cherno for this super fantastic explanation!

  • @Vermilicious
    @Vermilicious 4 года назад

    Passing functions is super useful sometimes. It's a concept I first saw using JavaScript. It's really cool that you can do it in C++ (for a while) now.

  • @jannesopanen8032
    @jannesopanen8032 3 года назад

    This is very handy matter in C++. I can pass a pointer as a parameter, pointer to function which I want to be in charge of handling values. :D

  • @JesseLeeHumphry
    @JesseLeeHumphry 2 года назад

    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.

  • @haven4512
    @haven4512 4 года назад

    Thanks for making these videos, they really help a lot!

  • @WhosTheBossHD
    @WhosTheBossHD 5 лет назад

    In C# i use Action for function pointers. Function pointers are great for Publish/Subscription type of projects.

  • @mms0537
    @mms0537 4 года назад

    I respect you dude

  • @OlliS71
    @OlliS71 Месяц назад

    There's std::for_each ... which can take a function object *with* state, but not just a function-pointer.

  • @lucianbetke
    @lucianbetke 3 года назад

    Cool mister. A good explanation.

  • @AxElKo440
    @AxElKo440 6 лет назад +2

    Man, it's awesome, thank you so much for these lessons ♥

  • @zoronic6248
    @zoronic6248 4 года назад +6

    I know it's a bit late, but GODDAMN THAT HAIRCUT MAN!!!!! Everyone looks good with a fade and part

  • @balaramprajapati8870
    @balaramprajapati8870 3 года назад

    very good explanation

  • @danklabunde
    @danklabunde 4 года назад

    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!

  • @Simon-fu8sd
    @Simon-fu8sd 2 года назад

    I just find this so cool

  • @lemonextract1325
    @lemonextract1325 Год назад

    thank you

  • @frederickmloka5454
    @frederickmloka5454 5 лет назад

    Thanks for demystifying subject

  • @isaiaskaung8856
    @isaiaskaung8856 6 лет назад

    Thank you so much for showing new tricks.

  • @chsu_
    @chsu_ Год назад

    Thanks for your video!

  • @zyulaikhaerhad
    @zyulaikhaerhad 3 года назад

    Thank you!! You just saved me and my assignment from the due date

  • @ianpan0102
    @ianpan0102 3 года назад

    Brilliant content. I can't thank you enough, Cherno!

  • @naughtrussel5787
    @naughtrussel5787 6 лет назад

    Thank you for cool vids. Plz don't stop.

  • @avnishs.2514
    @avnishs.2514 5 лет назад

    Amazing video! Helped me understand better

  • @daveski084
    @daveski084 5 лет назад +4

    He's so cute, makes learning C++ that much better lol.

  • @MohdFaisal-ie6mo
    @MohdFaisal-ie6mo 6 лет назад

    Keep up the good work. RUclips need experience coders like you.

  • @masterproducts0
    @masterproducts0 6 лет назад

    The simple explaination makes it very helpful! :) Thank you for this series and keep going like you do.

  • @samuelmaucaille702
    @samuelmaucaille702 6 лет назад

    Bro you're a fantastic teacher! thanks a lot!

  • @hankstalica1922
    @hankstalica1922 4 года назад

    Excellent vid.

  • @reductor_
    @reductor_ 5 лет назад

    This code is wrong, there is guarantee that 's_Finished' will be read again within the 'DoWorker', while the 'std::cout' does enough for it to likely have caused a memory barrier for it to work and also enough for the compiler to not optimize out the read.
    Before anyone says using volatile will fix this, volatile does not give any memory barriers, its just going to ensure the data was written or read by the compiler

  • @Decco6306
    @Decco6306 4 года назад

    wow
    A Programmer with awesome video quality that doenst use a Mac
    I appreciate that

  • @foomoo1088
    @foomoo1088 2 года назад +2

    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)

    • @Raul-vg3wt
      @Raul-vg3wt 2 года назад +1

      Would you be able to explain the details?? I haven't had much luck looking an answer up. Running into issue with this exact problem.

    • @foomoo1088
      @foomoo1088 2 года назад +1

      @@Raul-vg3wt I’ll try to post an example this weekend (crunching this week)

    • @Raul-vg3wt
      @Raul-vg3wt 2 года назад +1

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

  • @LAB_XI
    @LAB_XI 10 месяцев назад

    Great as usual 🔥

  • @JoCS11152
    @JoCS11152 Год назад

    mate i fucking love your series

  • @pmcgee003
    @pmcgee003 2 года назад

    Showing functional (trailing return) style might give another way to think of it : auto (*foo)( int ) -> void;

  • @danielc4267
    @danielc4267 6 лет назад

    Great content. Thank you cherno

  • @noitibmar
    @noitibmar 6 лет назад +1

    would function pointers be used for something like buttons in UI rather than having to make a class for every button that does something different?

  • @jsnmad
    @jsnmad 4 года назад

    C++ Callbacks! Nice

  • @popvic7666
    @popvic7666 Год назад

    i wanted just to add a more reason for function pointer: a function can't call a function as a parameter but can call a pointer to a function;

  • @voiddeepstorage6533
    @voiddeepstorage6533 3 года назад

    you should do a video about using variadic templates function pointers, its a really cool topic.

  • @zackjandali
    @zackjandali 5 лет назад +1

    @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

  • @PflanzenChirurg
    @PflanzenChirurg 6 лет назад

    Im so in love with u that u continue this series

  • @andreskoszutski4364
    @andreskoszutski4364 3 года назад

    .... in the future...
    Just kidding. I find your series extremely useful.

  • @gvcallen
    @gvcallen 4 года назад +1

    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

    • @eventhisidistaken
      @eventhisidistaken 4 года назад

      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.

    • @gvcallen
      @gvcallen 4 года назад

      @@eventhisidistaken his code was merely demonstrative. So doesnt really matter if it's "good" or "bad"

    • @eventhisidistaken
      @eventhisidistaken 4 года назад

      @@gvcallen It's demonstrative of what *not* to do.

  • @work_tech_stuff5563
    @work_tech_stuff5563 4 месяца назад +1

    Because we are in C++, it makes sense to see how this works with member functions. Thats why i am actually here but i don't see it.

  • @VasaMusic438
    @VasaMusic438 6 лет назад +1

    Great video, again !!! Thanks !!!!

  • @laurentiudobrescu9045
    @laurentiudobrescu9045 6 лет назад

    Good work ! I need this video , it's awesome !

  • @joseponce6250
    @joseponce6250 3 года назад

    thank you!!

  • @gunrunjk
    @gunrunjk 6 лет назад

    I always enjoy your video.

  • @dpalmer4588
    @dpalmer4588 5 лет назад

    So, like nested functions, essentially? very cool