function-pointers

How to use procedure pointers for subroutines with different number of arguments

帅比萌擦擦* 提交于 2019-12-11 14:36:55
问题 I'm developing a Fortran program for scientific computing. I want to use procedure pointers to assign the boundary conditions in the problem, shown in the following main program program main use boundary implicit none bc1 => boundaryA bc2 => boundaryB bc3 => boundaryC call comp_boundary end program I define all the boundary operations "boundaryA/B/C" in a "boundary" module module boundary implicit none procedure(boundary_type), pointer :: bc1,bc2,bc3 abstract interface subroutine boundary

Can we turn such structure into typed class/function?

假装没事ソ 提交于 2019-12-11 13:57:46
问题 So in structure like struct RenderCastedDataFunctor { simpleRendererGraphElement* obj_; RenderCastedDataFunctor(simpleRendererGraphElement* obj) : obj_(obj) { } void operator()(char* castedChar, int castedCharLength) { obj_->renderCastedData(castedChar, castedCharLength); } }; can we turn simpleRendererGraphElement* into abstract type and make its function name we use in structure ( renderCastedData ) abstract too? So I have a function inside charGenerator class template <typename Function>

Why must function pointers be used?

核能气质少年 提交于 2019-12-11 13:52:05
问题 What is the need for function pointers? The standard answer for this seems to be callbacks, but why can't we just pass a function? The book I was reading on C++ demonstrates passing a function as a parameter, and acknowledges that in actual fact the compiled turns this into a function pointer and passes that instead, because functions are not actual objects. It showed the equivalent code using function pointers, which was slightly more complex - if the code is equivalent, why bother to use a

Typecast for qsort function pointer

强颜欢笑 提交于 2019-12-11 13:37:05
问题 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> static int cmpstringp(const void *p1, const void *p2) { /* The actual arguments to this function are "pointers to pointers to char", but strcmp(3) arguments are "pointers to char", hence the following cast plus dereference */ return strcmp(* (char * const *) p1, * (char * const *) p2); } int main(int argc, char *argv[]) { int j; assert(argc > 1); qsort(&argv[1], argc - 1, sizeof(argv[1]), cmpstringp); for (j = 1; j

Double function indirection in C [duplicate]

感情迁移 提交于 2019-12-11 12:06:07
问题 This question already has answers here : How do function pointers in C work? (11 answers) Closed 3 years ago . I am writing a bootloader for an ARM Cortex-M0 CPU. I need to forward the IRQs to the target app, unfortunately the IRQ vector in this CPU is at fixed address and I cannot relocate it, so I need a bit of trickery. I know the address where the addresses of the target app's IRQs are stored. So let's say, at address 0x5008 there's the address of void x_IRQHandler(void) (target app) What

Partial class specialization for function pointer type and value

时光毁灭记忆、已成空白 提交于 2019-12-11 10:40:57
问题 I'm using FLTK to do my GUI related stuff, and it requires functions of type void (*fn)( Fl_Widget*, void* ) to be registered as widget callbacks. I'm tired of creating function forwarders by hand that unpack the void* s into parameters and call appropriate static functions from my classes to do the work the user has requested. I came up with a solution, but it requires a class to be specialized for both function type, and function address, and this is where I'm having problems. Here's some

one class should invoke method of another class using a function pointer

南笙酒味 提交于 2019-12-11 10:33:25
问题 I have consult (disclaimer): Class member function pointer Calling C++ class methods via a function pointer C++: Function pointer to another class function To illustrate my problem I will use this code (UPDATED) #include <iostream> #include <cmath> #include <functional> class Solver{ public: int Optimize(const std::function<double(double,double>& function_to_optimize),double start_val, double &opt_val){ opt_val = function_to_optimize(start_val); return 0; } }; class FunctionExample{ public:

C/C++, troubles with pointers and functions

心不动则不痛 提交于 2019-12-11 10:12:14
问题 I have a little question about understand how pointers and functions work. I want to see how a function looks like qsort() , but I need to use my own function to swap elements and to compare elements. I am very surprised to know that my function does not swap data... My code: //prototypes file: other.h void Sort(char* pcFirst, int nNumber, int size, void (*Swap)(void*, void*), int (*Compare)(void*, void*) ); //sorts any arrays void SwapInt(void* p1, void* p2); // swap pointers int CmpInt(void

Save and load function pointers to file

折月煮酒 提交于 2019-12-11 10:09:23
问题 Consider the following code: typedef float (*MathsOperation)(float _1, float _2); struct Data { float x, y; MathsOperation op; }; Data data[100]; float Add(float _1, float _2){//add} float Sub(float _1, float _2){//subtract} float Mul(float _1, float _2){//multiply} // other maths operations for (int i = 0; i < 100; ++i) { // assign one of the maths operators above to data struct member op // according to some condition (maybe some user input): if(condition1) data[i].op = &Add; if(condition2)

How do I pass a function pointer delegate for a different class in c#

半城伤御伤魂 提交于 2019-12-11 08:54:04
问题 In c++, To a function that takes in a function pointer with void return type, eg: void TakesFun(Func<void ()> fun ){ fun(); } Above function can be called in these ways //if foo is a function returning void but is declared in global space and not part of another class TakesFun(bind(foo)); //if foo is a function returning void but is declared in class called ClassX and the function is required to be called for object "obj". TakesFun(bind(ClassX::foo, obj)); //if foo is a function taking an