function-pointers

Function Pointers with Different Return Types C

丶灬走出姿态 提交于 2020-01-29 17:45:25
问题 I understand what function pointers are in C as well as how to use them. However, I do not know how to have one function pointer that can point to functions with different return types. Is this possible? I know how to use an array of function pointers, but I have only found examples of different parameters, not return types. (C: How can I use a single function pointer array for functions with variable parameter counts?) If an array would make more sense, can you provide an example of how to

Complex C declaration

流过昼夜 提交于 2020-01-26 21:53:28
问题 I was just going through some code on the Internet and found this: float * (*(*foo())[SIZE][SIZE])() How do I read this declaration? Is there a specific set of rules for reading such complex declarations? 回答1: I haven't done this in a while! Start with foo and go right. float * (*(* foo() )[SIZE][SIZE])() foo is a function with no arguments... Can't go right since there's a closing parenthesis. Go left: float * (*( * foo() )[SIZE][SIZE])() foo is a function with no arguments returning a

Why is it allowed to static_cast a method of a derived class to a method of the base class?

核能气质少年 提交于 2020-01-24 19:33:10
问题 example struct B1{int x; void f(){x = 1;}}; struct D : B1{int x; void f(){B1::x = 2;}}; using Dmp = void(D::*)(); using B1mp = void(B1::*)(); int main() { Dmp dmp = &D::f; D d; (d.*dmp)(); // ok B1mp b1mp = static_cast<B1mp>(dmp); // hm, well that's weird B1 b1; (b1.*b1mp)(); dmp = &B1::f; // ok } And this example will compile and run just fine, and no problem will arise. But wait, now I'm going to use D::x in D::f , and now -- anything can happen at runtime. Yes, you can also static_cast a

Why pointer to function is equal to 1? [duplicate]

南笙酒味 提交于 2020-01-24 12:02:31
问题 This question already has answers here : How to print function pointers with cout? (7 answers) Closed 2 years ago . Check following code: #include <iostream> using namespace std; int& foo() { static int i = 0; return i; } int main() { cout << &foo() << endl; cout << &foo << endl; return 0; } As you see, the first cout prints address of return value of foo() which will be static variable i inside foo() . For 2nd cout I was expecting that &foo returns address of foo() function, as stated here:

Why pointer to function is equal to 1? [duplicate]

吃可爱长大的小学妹 提交于 2020-01-24 12:02:25
问题 This question already has answers here : How to print function pointers with cout? (7 answers) Closed 2 years ago . Check following code: #include <iostream> using namespace std; int& foo() { static int i = 0; return i; } int main() { cout << &foo() << endl; cout << &foo << endl; return 0; } As you see, the first cout prints address of return value of foo() which will be static variable i inside foo() . For 2nd cout I was expecting that &foo returns address of foo() function, as stated here:

Setting a std::function variable to refer to the std::sin function

こ雲淡風輕ζ 提交于 2020-01-22 19:38:29
问题 I've got a question about how to properly use the new C++11 std::function variable. I've seen several examples from searching the Internet, but they don't seem to cover the usage case I'm considering. Take this minimum example, where the function fdiff is an implementation of the finite forward differencing algorithm defined in numerical.hxx (which isn't the problem, I just wanted to give a contextual reason why I'd want to take an arbitrary function and pass it around). #include <functional>

Printing function pointer passed as a parameter results in '1' printed on the screen

孤人 提交于 2020-01-21 10:20:24
问题 I've been experimenting with function pointers and found the behavior of the following program rather misterious: void foo(int(*p)()) { std::cout << p << std::endl; } int alwaysReturns6() { return 6; } int main() { foo(alwaysReturns6); return 0; } The above code prints the number '1' on the screen. I know I should access the function pointer like this: p() (and then 6 gets printed), but I still don't get what the plain p or *p means when used in the foo function. 回答1: std::cout << p << std:

Printing function pointer passed as a parameter results in '1' printed on the screen

本秂侑毒 提交于 2020-01-21 10:20:17
问题 I've been experimenting with function pointers and found the behavior of the following program rather misterious: void foo(int(*p)()) { std::cout << p << std::endl; } int alwaysReturns6() { return 6; } int main() { foo(alwaysReturns6); return 0; } The above code prints the number '1' on the screen. I know I should access the function pointer like this: p() (and then 6 gets printed), but I still don't get what the plain p or *p means when used in the foo function. 回答1: std::cout << p << std:

Captureless lambda cannot be converted to function pointer when stored in std::function

我只是一个虾纸丫 提交于 2020-01-21 01:14:29
问题 Usually, a C++ lambda without a capture should be convertable to a c-style function pointer. Somehow, converting it using std::function::target does not work (i.e. returns a nullptr), also the target_type does not match the signature type even though it seems to be the same. Tested on VC13 and GCC 5.3 / 5.2.0 / 4.8 Minimal testing example: #include <functional> #include <iostream> void Maybe() { } void callMe(std::function<void()> callback) { typedef void (*ftype)(); std::cout << (callback

How to get class's function pointer for non-static function from another class's function?

倖福魔咒の 提交于 2020-01-16 18:23:47
问题 I need to pass class function as argument for glutIdleFunc. This construction doesn't work: void (*func)(void) = this->step(); glutIdleFunc(func); and this too: void (*func)(void) = Core::step(); glutIdleFunc(func); How can I do it? Sorry for my stupidity... :) 回答1: In C++, callbacks such as the one accepted by glutIdleFunc() have to be static methods. Try: class Core { public: static void step() { // ... } }; glutIdleFunc(Core::step); 回答2: glutIdleFunc simply does not support calling a non