function-pointers

Why would one use function pointers to member method in C++?

不羁的心 提交于 2019-12-03 08:32:26
A lot of C++ books and tutorials explain how to do this, but I haven't seen one that gives a convincing reason to choose to do this. I understand very well why function pointers were necessary in C (e.g., when using some POSIX facilities). However, AFAIK you can't send them a member function because of the "this" parameter. But if you're already using classes and objects, why not just use an object oriented solution like functors? Real world examples of where you had to use such function pointers would be appreciated. Update: I appreciate everyone's answers. I have to say, though, that none of

NSComparisonResult and NSComparator - what are they?

☆樱花仙子☆ 提交于 2019-12-03 07:24:24
What is NSComparisonResult and NSComparator ? I've seen one of the type definitions, something like that: typedef NSComparisonResult (^NSComparator)(id obj1, id obj2); Is it any different from a function pointer? Also, I can't even guess what the ^ symbol means. ^ signifies a block type , similar in concept to a function pointer. typedef NSComparisonResult (^NSComparator)(id obj1, id obj2); // ^ ^ ^ // return type of block type name arguments This means that the type NSComparator is a block that takes in two objects of type id called obj1 and obj2 , and returns an NSComparisonResult .

Why can I invoke a function via a pointer with too many arguments?

萝らか妹 提交于 2019-12-03 06:49:55
问题 Say I have this function: int func2() { printf("func2\n"); return 0; } Now I declare a pointer: int (*fp)(double); This should point to a function that takes a double argument and returns an int . func2 does NOT have any argument, but still when I write: fp = func2; fp(2); (with 2 being just an arbitrary number), func2` is invoked correctly. Why is that? Is there no meaning to the number of parameters I declare for a function pointer? 回答1: Yes, there is a meaning. In C (but not in C++), a

Any way to generate warnings for function-pointer comparisons?

只愿长相守 提交于 2019-12-03 06:37:57
问题 It took me forever to track down that there was a bug in my code being triggered by /OPT:ICF : Because /OPT:ICF can cause the same address to be assigned to different functions or read-only data members (const variables compiled by using /Gy), it can break a program that depends on unique addresses for functions or read-only data members. (I had been storing and comparing function pointers for equality, which breaks when the linker throws away identical functions.) Now I need to find every

Determining to which function a pointer is pointing in C?

一笑奈何 提交于 2019-12-03 06:11:54
问题 I have a pointer to function, assume any signature. And I have 5 different functions with same signature. At run time one of them gets assigned to the pointer, and that function is called. Without inserting any print statement in those functions, how can I come to know the name of function which the pointer currently points to? 回答1: You will have to check which of your 5 functions your pointer points to: if (func_ptr == my_function1) { puts("func_ptr points to my_function1"); } else if (func

How to recursively dereference pointer (C++03)?

折月煮酒 提交于 2019-12-03 05:35:25
I'm trying to recursively dereference a pointer in C++. If an object is passed that is not a pointer (this includes smart pointers), I just want to return the object itself, by reference if possible. I have this code: template<typename T> static T &dereference(T &v) { return v; } template<typename T> static const T &dereference(const T &v) { return v; } template<typename T> static T &dereference(T *v) { return dereference(*v); } My code seems to work fine in most cases, but it breaks when given function pointers , because dereferencing a function pointer results in the same exact type of

Is it possible to take the address of an ADL function?

六月ゝ 毕业季﹏ 提交于 2019-12-03 05:30:46
Is it possible to take the address of a function that would be found through ADL? For example: template<class T> void (*get_swap())(T &, T &) { return & _________; // how do I take the address of T's swap() function? } int main() { typedef some_type T; get_swap<T>(); } Cassio Neri Honestly, I don't know but I tend towards saying that this is not possible. Depending on what you want to achieve I can suggest a workaround. More precisely, if you just need the address of a function that has the same semantics as swap called through ADL then you can use this: template <typename T> void (*get_swap()

how to use std::function to point to a function template

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 05:15:11
#include <functional> int func(int x, int y) { return x+y; } int main() { typedef std::function<int(int, int)> Funcp; Funcp funcp = func; return 0; } But is it possible to point to a template function? #include <functional> template<class T> T func(T x, T y) { return x+y; } int main() { typedef std::function<?(?, ?)> Funcp; Funcp funcp = func; return 0; } No. A template function is exactly that, a template. It's not a real function. You can point a std::function to a specific instantiation of the template function, e.g. func<int,int> There is no such thing as a template function in C++ — what

Are functors actually faster than pointers to functions?

倖福魔咒の 提交于 2019-12-03 04:02:37
问题 According to Scott Meyers, one area where C++ shines over C is that function objects are faster than function pointers. He says this is because function objects are inlined, which increases speed. I have two questions about this: How can we verify that function objects are, in fact, inlined? Can we verify this in practice? Does the inlining of function objects depend on the compiler we use, or do all compilers behave like this? 回答1: The C++ and C standards leaves a bunch of freedom to

What's the difference between a pointer, and a pointer variable?

 ̄綄美尐妖づ 提交于 2019-12-03 02:44:58
I know this is very basic but it is little bit confusing to me. I've read: a pointer is nothing more than an address , and a pointer variable is just a variable that can store an address . When we store the address of a variable i in the pointer variable p , we say that p points to i . int i, *p = &i; p points to i . To gain access to the object that a pointer points to, we use the * (indirection) operator. If p is a pointer then *p represents the object to which p currently points. Now I am confused that what should I call p -- pointer or pointer variable ? Additional: Is a pointer always the