Function pointers, typedef a function pointer, and std::function | Modern Cpp Series Ep. 30

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

►Lesson Description: In this lesson I show how to create a variable that can store a function address--this is called a function pointer. One common use of function pointers is to be used for 'callback' functions that can be reassigned at run-time to change the behavior associated with some object (e.g. in a graphical user interface when you press a button).

Later on in the video I'll show a few easier ways to work with function pointers, including using a 'typedef' (which you'll often see in C codebases), as well as the more modern std::function, which uses a much more forgiving syntax.

►Please like and subscribe to help the channel!
Рекомендации по теме
Комментарии
Автор

Hi ​ @MikeShah Can you please explain the typedef part, because as I know typedef works as, e.g.: typedef <current_name> <new_name>, but in your Function pointer example, you just mentioned <current_name> and no <new_name>, then how you were able to make "op" as some kind of type of this typedef function pointer?

praveenpragya
Автор

Thank you so much, I've watched tens of these videos, all explaining how to use std::function very poorly. Instead they explain all kinds of irrelevant things and get sidetracked. This was super clear and to the point! You've got a new subscriber.

tommyholmberg
Автор

Thank you for this video Mike. You can really tell you teach at university level because your explanations are so easy to understand.

ifarazc
Автор

I came from you extensible factory design cuz i was not getting the callback, and man I'm happy that I found another video of yours explaining it. Amazing job per usual!

lighr
Автор

Very cool tutorials. I try to learn by myself but there’s a lot of ways to do one thing and it makes me confused. Now I understand their relationship and it’s great. Thanks a lot!

trading_for_living
Автор

Finally I understood this topic! Thank you!

jeordanisfiguereo
Автор

Thank you, best function pointer explanation.

amitagupta
Автор

Mike, can you give an example that shows where it's necessary to use a pointer to a function? Why can't I just call the function directly in all cases?

bufet
Автор

Very neatly explained as always. Thank You.

dhanushs
Автор

I love the videos Mike and thank you.

This explanation of a pointer being used in a function helped me understand why a pointer is used, although I may be wrong here about my assumption, I would have to see if the pointer being used here is using the same memory address for each operation of: add and multiply.

By using one address location in the memory, we efficiently and effectively maintain where the memory exists and is being used; likewise, doing these operations without a pointer would make use of two different memory addresses when running the program, essentially using more memory to run the program.

Please correct me if I am wrong, but maybe this is why we use pointers in c++?

chabra
Автор

It's interesting, that in some newer C-based languages such as 'C3', function pointers types cannot be used 'raw', but must always be 'wrapped' with a type alias - which can make the code more readable by giving meaningful names to the function pointer types, and it allows you to change the function signature in just one place if needed.

GaryChike
Автор

I also like using std::add_pointer from <type_traits> to create function pointer type aliases.
using BinaryFunc = std::add_pointer_t<int (int, int)>;
BinaryFunc pAdd = &Add;

ImmortalityYT
Автор

Thanks so much! Would you talk about data structure and algorithm in the future? :D

k
Автор

Great and clear explanation! I have two quick questions. First, compared to the function pointer, what is the extra performance cost and overhead of std::function? Second, when we pass std::function to another function as an argument, like an event handler which will call this std::function at some future time, should we pass this std::function by value or by const reference? I think both options are correct but will there be any difference in performance?

charliezhao
Автор

thanks for sharing. I have a question that, if I use std::function for declaration a function in .h file, like (std::function<void(int, int)> functionName), how to write definition for it in .cpp file?? (they all in the same class let's say class A)

SummerKRock
Автор

I have a small doubt that sometime its written type funcName() Const; is that same as const type funcName()?

jugalshah
Автор

Function pointer and std::function are not exactly the same. To use the std::function instead of a normal function pointer is a bad habit, since std::functions have a much bigger overhead!

rombrandsdg
Автор

Where is the callback function? u didnt demonstrated it.

visitor_t-wp