Function Pointers in C++

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 พ.ย. 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.

  • @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.

  • @PythonPlusPlus
    @PythonPlusPlus 6 ปีที่แล้ว +583

    Who else is binge watching the playlist?

    • @Caighy
      @Caighy 6 ปีที่แล้ว +3

      yup.

    • @smurf123444
      @smurf123444 6 ปีที่แล้ว +4

      me!

    • @abdulalhazred5924
      @abdulalhazred5924 5 ปีที่แล้ว +17

      only you, I'm just looking for some specific stuff

    • @edencheung8468
      @edencheung8468 5 ปีที่แล้ว

      Mememememe

    • @RamiAwar
      @RamiAwar 4 ปีที่แล้ว +7

      > Netflix

  • @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!

  • @BlackJar72
    @BlackJar72 6 ปีที่แล้ว +212

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

    • @louarn
      @louarn 6 ปีที่แล้ว +39

      It's C++, not A+++++.

    • @richardlighthouse5328
      @richardlighthouse5328 6 ปีที่แล้ว

      @Louarn he meant grade: A+++++

    • @louarn
      @louarn 6 ปีที่แล้ว +32

      @@richardlighthouse5328 It was a joke

    • @vertigo6982
      @vertigo6982 6 ปีที่แล้ว +1

      Now on to function objects, and functors!

    • @diamaudixaudioltd.299
      @diamaudixaudioltd.299 2 ปีที่แล้ว +1

      needs more +'s

  • @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.

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

  • @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.

  • @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.

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

  • @AswinJP
    @AswinJP 4 ปีที่แล้ว +5

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

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

  • @iteratedofficial
    @iteratedofficial 3 ปีที่แล้ว

    Probably the best c++ tutor on youtube

  • @annahakobian7371
    @annahakobian7371 5 ปีที่แล้ว +3

    best c++ tutorials, thanks man

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

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

  • @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.

  • @hlrbBrambleX
    @hlrbBrambleX 3 ปีที่แล้ว

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

  • @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....

  • @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.

  • @muhammadarslanrana942
    @muhammadarslanrana942 4 ปีที่แล้ว +3

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

  • @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 🙏🏾

  • @duchaolv5876
    @duchaolv5876 3 ปีที่แล้ว

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

  • @Fol2ey
    @Fol2ey 4 ปีที่แล้ว

    Dude. Your explanations are saving my life right now!

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

  • @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.

  • @onyinyechichukwuma689
    @onyinyechichukwuma689 3 ปีที่แล้ว

    The best C++ explanations EVER!
    Thanks!

  • @hishamsomroz6913
    @hishamsomroz6913 3 ปีที่แล้ว

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

  • @diegomora997
    @diegomora997 2 ปีที่แล้ว +1

    Dude, you make me feel more exited about computer science

  • @GenericPhantom1
    @GenericPhantom1 ปีที่แล้ว

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

  • @NeoKailthas
    @NeoKailthas 3 ปีที่แล้ว +5

    "Gentle introduction" lol 😆

  • @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.

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

  • @DiogoManteu
    @DiogoManteu ปีที่แล้ว

    Such a great explanation with examples.

  • @jakobhuetter
    @jakobhuetter 3 ปีที่แล้ว +1

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

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

  • @robfei-u6b
    @robfei-u6b ปีที่แล้ว

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

  • @AngelTaylorgang809
    @AngelTaylorgang809 4 ปีที่แล้ว +3

    Start a data structure series please, greetings from Texas

  • @nandukrishna8142
    @nandukrishna8142 2 ปีที่แล้ว

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

  • @zoronic6248
    @zoronic6248 4 ปีที่แล้ว +6

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

  • @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.

  • @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.

  • @haven4512
    @haven4512 4 ปีที่แล้ว

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

  • @nuriya9000
    @nuriya9000 5 ปีที่แล้ว

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

  • @charr6108
    @charr6108 2 ปีที่แล้ว

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

  • @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!

  • @qy4247
    @qy4247 3 ปีที่แล้ว

    Really good tutorial. Thx from China ❤

  • @momentswithjireh3921
    @momentswithjireh3921 3 ปีที่แล้ว

    Thank you Cherno for this super fantastic explanation!

  • @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.

  • @MohdFaisal-ie6mo
    @MohdFaisal-ie6mo 6 ปีที่แล้ว

    Keep up the good work. TH-cam need experience coders like you.

  • @AxElKo440
    @AxElKo440 6 ปีที่แล้ว +2

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

  • @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!

  • @daveski084
    @daveski084 5 ปีที่แล้ว +4

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

  • @ianpan0102
    @ianpan0102 3 ปีที่แล้ว

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

  • @naughtrussel5787
    @naughtrussel5787 6 ปีที่แล้ว

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

  • @santhoshnagarajs3489
    @santhoshnagarajs3489 6 ปีที่แล้ว

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

  • @zyulaikhaerhad
    @zyulaikhaerhad 3 ปีที่แล้ว

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

  • @masterproducts0
    @masterproducts0 6 ปีที่แล้ว

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

  • @andrellmarrow4642
    @andrellmarrow4642 9 วันที่ผ่านมา

    super informative. thanks man

  • @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;

  • @mms0537
    @mms0537 4 ปีที่แล้ว

    I respect you dude

  • @PflanzenChirurg
    @PflanzenChirurg 6 ปีที่แล้ว

    Im so in love with u that u continue this series

  • @JoCS11152
    @JoCS11152 ปีที่แล้ว

    mate i fucking love your series

  • @Simon-fu8sd
    @Simon-fu8sd 2 ปีที่แล้ว

    I just find this so cool

  • @samuelmaucaille702
    @samuelmaucaille702 6 ปีที่แล้ว

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

  • @WhosTheBossHD
    @WhosTheBossHD 5 ปีที่แล้ว

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

  • @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.

  • @OlliS71
    @OlliS71 หลายเดือนก่อน

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

  • @leducphuclong
    @leducphuclong 7 หลายเดือนก่อน

    Thank you so much !! Great Teacher !!

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

  • @Decco6306
    @Decco6306 4 ปีที่แล้ว

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

  • @jsnmad
    @jsnmad 4 ปีที่แล้ว

    C++ Callbacks! Nice

  • @LAB_XI
    @LAB_XI 10 หลายเดือนก่อน

    Great as usual 🔥

  • @avnishs.2514
    @avnishs.2514 5 ปีที่แล้ว

    Amazing video! Helped me understand better

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

  • @chsu_
    @chsu_ ปีที่แล้ว

    Thanks for your video!

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

  • @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.

  • @voiddeepstorage6533
    @voiddeepstorage6533 3 ปีที่แล้ว

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

  • @lucianbetke
    @lucianbetke 3 ปีที่แล้ว

    Cool mister. A good explanation.

  • @andreskoszutski4364
    @andreskoszutski4364 3 ปีที่แล้ว

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

  • @lemonextract1325
    @lemonextract1325 ปีที่แล้ว

    thank you

  • @danielc4267
    @danielc4267 6 ปีที่แล้ว

    Great content. Thank you cherno

  • @laurentiudobrescu9045
    @laurentiudobrescu9045 6 ปีที่แล้ว

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

  • @frederickmloka5454
    @frederickmloka5454 5 ปีที่แล้ว

    Thanks for demystifying subject

  • @pmcgee003
    @pmcgee003 2 ปีที่แล้ว

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

  • @isaiaskaung8856
    @isaiaskaung8856 6 ปีที่แล้ว

    Thank you so much for showing new tricks.

  • @balaramprajapati8870
    @balaramprajapati8870 3 ปีที่แล้ว

    very good explanation

  • @VasaMusic438
    @VasaMusic438 6 ปีที่แล้ว +1

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

  • @gunrunjk
    @gunrunjk 6 ปีที่แล้ว

    I always enjoy your video.

  • @RX7GSLSEowner
    @RX7GSLSEowner 6 ปีที่แล้ว +11

    "So instead of this kind of weird code..." - hahahaha! Yeah to those coming to C++ from C# or Java (or the like) or newbie programmers, I suspect that you are correct. For us old-school C programmers though function pointer syntax such as you illustrated is trivial to our eyes. In that world I am somewhat of an outlier because I happen to know C++ as well. That said, we do what I call "poor man's OO programming" in the sense that we combine data and methods into structs, where "methods" are function pointers that are initialized during some type of "create" invocation, which is a poor-man's rough equivalent of a constructor. The methods (function pointer lists) are grouped into what we call a "method table", then through macros those functions which are assigned to the corresponding function pointers, which are veiled as "methods" are invoked directly by the user, which gives the appearance OO style programming, though not really. Kinda. Anyway I digress. Thank you for the late night chuckle. lol Good video, keep up the work!

    • @kamilkarwacki9590
      @kamilkarwacki9590 6 ปีที่แล้ว +3

      I was always undef the impressiom that there has to be some kind of OO even in procedual languages, thanks for the insights

    • @syedbaryalay5849
      @syedbaryalay5849 6 ปีที่แล้ว +3

      coming from JavaScript and feeling very much at home, except not with the syntax.

    • @Mateus.007
      @Mateus.007 4 ปีที่แล้ว

      @@kamilkarwacki9590 Yeah. Actually oop languages uses functions pointers in objects.
      object.action() is the same >> function_action(&struct)

  • @alexsece
    @alexsece 5 ปีที่แล้ว

    Great job!

  • @shahadalrawi6744
    @shahadalrawi6744 9 หลายเดือนก่อน

    The life savior

  • @lorenzodiazdonzelli3048
    @lorenzodiazdonzelli3048 3 ปีที่แล้ว

    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

    • @lorenzodiazdonzelli3048
      @lorenzodiazdonzelli3048 3 ปีที่แล้ว

      also, shouldn't the & sign be before the name of the param and not after the type or is it the same?

  • @tokita1230
    @tokita1230 6 ปีที่แล้ว

    I hope that you'll talk about method pointers in the next video and how you can use them without the standard library.