function-pointers

Class member function pointer

╄→гoц情女王★ 提交于 2019-11-28 18:29:24
I'm trying to use a class function (interrupt service routine), void (ClassName::*fp)(void)=ClassName::FunctionName; and attaching it to an Arduino interrupt pin using the function with the following type inputs but that doesn't work. void attachInterrupt(int, void (*)(void),int); How can I make this happen? The interrupt service routine (ISR) needs to access privat object data, so I can't make a function outside of the class. My compiler error: ClassName.cpp : : In constructor 'ClassName::ClassName()': ClassName.cpp : *)()' ClassName.cpp : *)()' to 'void (*)()' for argument '2' to 'void

c++ Implementing Timed Callback function

喜欢而已 提交于 2019-11-28 18:25:08
I want to implement some system in c++ so that I can call a function and ask for another function to be called in X milliseconds. Something like this: callfunctiontimed(25, funcName); 25 being the amount of milliseconds before the function should be called. I would like to know if multithreading is required for this and then use some delay function? Other than using function pointer how would a feature like this work? For a portable solution, you can use boost::asio. Below is a demo I wrote a while ago. You can change t.expires_from_now(boost::posix_time::seconds(1)); to suit you need say make

C++ Pointer to virtual function

孤街醉人 提交于 2019-11-28 18:24:16
If you have a struct like this one struct A { void func(); }; and a reference like this one A& a; you can get a pointer to its func method like this: someMethod(&A::func); Now what if that method is virtual and you don't know what it is at run-time? Why can't you get a pointer like this? someMethod(&a.func); Is it possible to get a pointer to that method? Pointers to members take into account the virtuality of the functions they point at. For example: #include <iostream> struct Base { virtual void f() { std::cout << "Base::f()" << std::endl; } }; struct Derived:Base { virtual void f() { std:

How to define typedef of function pointer which has template arguments

怎甘沉沦 提交于 2019-11-28 18:24:08
I would like to make typedef for function pointer which has stl container as argument and this container has unknown type. Something like this: typedef void (* TouchCallBack)(GLRenderer*, const MotionEvent&, std::vector<T >); it's possible? (especially in c++ 03) I don't know of any C++03 solution exactly like that, and it's not built into the language, but in C++11, this is possible with using aliases: template<typename T> using TouchCallBack = void (*)(GLRenderer*, const MotionEvent&, std::vector<T >); One workaround for C++03 is using a struct: template<typename T> struct TouchCallBack {

What is the address of a function in a C++ program?

泪湿孤枕 提交于 2019-11-28 18:13:45
As the function is set of instruction stored in one contiguous block of memory. And address of a function (entry point) is the address of the first instruction in the function. (from my knowledge) And thus we can say that the address of function and the address of the first instruction in the function will be the same (In this case the first instruction is the initialization of a variable.). But the program below contradicts the above line. code: #include<iostream> #include<stdio.h> #include<string.h> using namespace std; char ** fun() { static char * z = (char*)"Merry Christmas :)"; return &z

Usage and Syntax of std::function

五迷三道 提交于 2019-11-28 15:52:17
问题 It is necessary to me to use std::function but I don't know what the following syntax means. std::function<void()> f_name = []() { FNAME(); }; What is the goal of using std::function ? Is it to make a pointer to a function? 回答1: std::function is a type erasure object. That means it erases the details of how some operations happen, and provides a uniform run time interface to them. For std::function , the primary 1 operations are copy/move, destruction, and 'invocation' with operator() -- the

How does the C code that prints from 1 to 1000 without loops or conditional statements work?

萝らか妹 提交于 2019-11-28 14:59:00
I've found C code that prints from 1 to 1000 without loops or conditionals : But I don't understand how it works. Can anyone go through the code and explain each line? #include <stdio.h> #include <stdlib.h> void main(int j) { printf("%d\n", j); (&main + (&exit - &main)*(j/1000))(j+1); } Don't ever write code like that. For j<1000 , j/1000 is zero (integer division). So: (&main + (&exit - &main)*(j/1000))(j+1); is equivalent to: (&main + (&exit - &main)*0)(j+1); Which is: (&main)(j+1); Which calls main with j+1 . If j == 1000 , then the same lines comes out as: (&main + (&exit - &main)*1)(j+1);

Function pointer as parameter

爷,独闯天下 提交于 2019-11-28 14:43:24
问题 I try to call a function which passed as function pointer with no argument, but I can't make it work. void *disconnectFunc; void D::setDisconnectFunc(void (*func)){ disconnectFunc = func; } void D::disconnected(){ *disconnectFunc; connected = false; } 回答1: The correct way to do this is: typedef void (*callback_function)(void); // type for conciseness callback_function disconnectFunc; // variable to store function pointer type void D::setDisconnectFunc(callback_function pFunc) { disconnectFunc

Pointer to a C++ class member function as a global function's parameter?

余生长醉 提交于 2019-11-28 14:00:14
I have got a problem with calling a global function, which takes a pointer to a function as a parameter. Here is the declaration of the global function: int lmdif ( minpack_func_mn fcn, void *p, int m, int n, double *x, double *fvec, double ftol) The "minpack_func_mn" symbol is a typedef for a pointer to a function, defined as: typedef int (*minpack_func_mn)(void *p, int m, int n, const double *x, double *fvec, int iflag ); I want to call the "lmdif" function with a pointer to a function which is a member of a class I created, and here is the declaration of this class function: int LT

CUDA: Copy dynamically created array of function pointers on the CPU to GPU memory

喜夏-厌秋 提交于 2019-11-28 12:25:40
问题 I would like to create a list of function pointers dynamically on the CPU (with some sort of push_back() method called from main() ) and copy it to a GPU __constant__ or __device__ array, without needing to resort to static __device__ function pointers. I believe this question is related to my problem; however, my goal is to create the __host__ function pointer array iteratively and then copy it to the __constant__ function pointer array instead of initialising the latter on declaration. A