function-pointers

Pointer to current function

此生再无相见时 提交于 2019-12-29 08:51:29
问题 Is there any way to get a pointer to the current function, maybe through gcc extensions or some other trickery? Edit I'm curious whether it is possible to get the function pointer without ever explicitly using the function's name. I thought I had a good reason for wanting this, realized that I didn't really, but am still curious if it is possible. 回答1: This isn't especially portable, but should work on at least some platforms (i.e., Linux and OSX, where I can check the documentation; it

How do I refer to std::sin(const valarray<double> &)?

▼魔方 西西 提交于 2019-12-29 07:43:16
问题 I'm having trouble with some valarray function pointer code: double (*fp)(double) = sin; valarray<double> (*fp)(const valarray<double> &) = sin; The first compiles, the second gives: error: no matches converting function 'sin' to type 'class std::valarray<double> (*)(const class std::valarray<double>&)' 回答1: This compiles, using the __typeof__ GCC extension. Looks like GCC's valarray uses expression templates to delay calculation of the sinus. But that will make the return type of the sin

Do function pointers force an instruction pipeline to clear?

我只是一个虾纸丫 提交于 2019-12-28 12:15:42
问题 Modern CPUs have extensive pipelining, that is, they are loading necessary instructions and data long before they actually execute the instruction. Sometimes, the data loaded into the pipeline gets invalidated, and the pipeline must be cleared and reloaded with new data. The time it takes to refill the pipeline can be considerable, and cause a performance slowdown. If I call a function pointer in C, is the pipeline smart enough to realize that the pointer in the pipeline is a function pointer

C++, function pointer to the template function pointer

六眼飞鱼酱① 提交于 2019-12-28 11:47:28
问题 I am having a pointer to the common static method class MyClass { private: static double ( *pfunction ) ( const Object *, const Object *); ... }; pointing to the static method class SomeClass { public: static double getA ( const Object *o1, const Object *o2); ... }; Initialization: double ( *MyClass::pfunction ) ( const Object *o1, const Object *o2 ) = &SomeClass::getA; I would like to convert this pointer to the static template function pointer: template <class T> static T ( *pfunction ) (

Passing member function pointer to member object in c++

有些话、适合烂在心里 提交于 2019-12-28 05:10:33
问题 I have a problem with using a pointer to function in C++. Here is my example: #include <iostream> using namespace std; class bar { public: void (*funcP)(); }; class foo { public: bar myBar; void hello(){cout << "hello" << endl;}; }; void byebye() { cout << "bye" << endl; } int main() { foo testFoo; testFoo.myBar.funcP = &byebye; //OK testFoo.myBar.funcP = &testFoo.hello; //ERROR return 0; } Compilator returns an error at testFoo.myBar.funcP = &testFoo.hello; : ISO C++ forbids taking the

Dynamic method dispatching in C

半世苍凉 提交于 2019-12-28 03:38:14
问题 I know it sounds silly and I know that C is not an Object Oriented Language. But is there any way that dynamic method dispatching can be achieved in C? I thought about function pointers but don't get the entire idea. How could I implement this? 回答1: As others have noted, it is certainly possible to implement this in C. Not only is it possible, it is a fairly common mechanism. The most commonly used example is probably the file descriptor interface in UNIX. A read() call on a file descriptor

Why is using the function name as a function pointer equivalent to applying the address-of operator to the function name?

早过忘川 提交于 2019-12-27 11:04:33
问题 It's interesting that using the function name as a function pointer is equivalent to applying the address-of operator to the function name ! Here's the example. typedef bool (*FunType)(int); bool f(int); int main() { FunType a = f; FunType b = &a; // Sure, here's an error. FunType c = &f; // This is not an error, though. // It's equivalent to the statement without "&". // So we have c equals a. return 0; } Using the name is something we already know in array. But you can't write something

Why is using the function name as a function pointer equivalent to applying the address-of operator to the function name?

我的未来我决定 提交于 2019-12-27 11:04:03
问题 It's interesting that using the function name as a function pointer is equivalent to applying the address-of operator to the function name ! Here's the example. typedef bool (*FunType)(int); bool f(int); int main() { FunType a = f; FunType b = &a; // Sure, here's an error. FunType c = &f; // This is not an error, though. // It's equivalent to the statement without "&". // So we have c equals a. return 0; } Using the name is something we already know in array. But you can't write something

Using a function pointer from one class in a function of another class

£可爱£侵袭症+ 提交于 2019-12-25 11:55:28
问题 I have looked at the following threads, but they do not seem to help resolve the issue: Function pointer to member function - this thread does not help because the access is made within main and not another class one class should invoke method of another class using a function pointer - I could not follow the accepted answer as it is not clear to me how to use <functional> header. I have a U seful F unction class, UF , in which I declare and define all common utility functions for my program.

Reference level part 2

倾然丶 夕夏残阳落幕 提交于 2019-12-25 08:59:59
问题 Expanding on this question, it looks like I did not provide enough detail. I have an object called CallbackObject that is designed to contain an object instance, and the information of what function to invoke, and the parameters to pass at invokation time. template <typename objectType, typename memberFunctionPtrType, typename memberFcnArg1Type> struct CallbackObject1 { objectType obj ; memberFunctionPtrType fcnPtr ; memberFcnArg1Type arg1 ; CallbackObject1(objectType iObj,