function-pointers

Virtual Methods or Function Pointers

拜拜、爱过 提交于 2019-11-26 23:57:45
问题 When implementing polymorphic behavior in C++ one can either use a pure virtual method or one can use function pointers (or functors). For example an asynchronous callback can be implemented by: Approach 1 class Callback { public: Callback(); ~Callback(); void go(); protected: virtual void doGo() = 0; }; //Constructor and Destructor void Callback::go() { doGo(); } So to use the callback here, you would need to override the doGo() method to call whatever function you want Approach 2 typedef

Function pointer to class member function problems

谁都会走 提交于 2019-11-26 23:24:32
问题 First of all I have to admit that my programming skills are pretty limited and I took over a (really small) existing C++ OOP project where I try to push my own stuff in. Unfortunately I'm experiencing a problem which goes beyond my knowledge and I hope to find some help here. I'm working with a third party library (which cannot be changed) for grabbing images from a camera and will use some placeholder names here. The third party library has a function "ThirdPartyGrab" to start a continuous

How can I store function pointers in an array? [duplicate]

别来无恙 提交于 2019-11-26 23:20:35
问题 This question already has answers here : “Expected fn item, found a different fn item” when working with function pointers (2 answers) Closed 3 years ago . How do you stick functions (or function pointers) into an array for testing purposes? fn foo() -> isize { 1 } fn bar() -> isize { 2 } fn main() { let functions = vec![foo, bar]; println!("foo() = {}, bar() = {}", functions[0](), functions[1]()); } This code in the Rust playground This is the error code I get: error: mismatched types:

Is there an elegant way to avoid dlsym when using dlopen in C?

廉价感情. 提交于 2019-11-26 22:59:26
问题 I need to dynamically open a shared library lib.so if a specific condition is met at runtime. The library contains ~700 functions and I need to load all their symbols. A simple solution is to define the function pointers to all symbols contained in lib.so , load the library using dlopen and finally get the addresses of all symbols using dlsym . However, given the number of functions, the code implementing this solution is very cumbersome. I was wondering if a more elegant and concise solution

Javascript Function-Pointer Assignment

本秂侑毒 提交于 2019-11-26 22:35:09
问题 Consider this javascript code: var bar = function () { alert("A"); } var foo = bar; bar = function () { alert("B"); }; foo(); When running this code I get "A". Is this behavior a part of javascript specification and can I rely on it? 回答1: Yes that is expected and by design. Your question is basically: does foo reference bar as a pointer or reference would in another language? The answer is no: the value of bar at the time of assignment is assigned to foo . 回答2: In other examples, nothing was

Function pointers casting in C++

牧云@^-^@ 提交于 2019-11-26 22:07:01
I have a void pointer returned by dlsym(), I want to call the function pointed by the void pointer. So I do a type conversion by casting: void *gptr = dlsym(some symbol..) ; typedef void (*fptr)(); fptr my_fptr = static_cast<fptr>(gptr) ; I have also tried reinterpret_cast but no luck, although the C cast operator seems to work.. Faisal Vali Converting a void* to a function pointer directly is not allowed (should not compile using any of the casts) in C++98/03. It is conditionally supported in C++0x (an implementation may choose to define the behavior and if it does define it then it must do

How do I get the argument types of a function pointer in a variadic template class?

放肆的年华 提交于 2019-11-26 22:05:57
This is a follow up of this problem: Generic functor for functions with any argument list I have this functor class (full code see link above): template<typename... ARGS> class Foo { std::function<void(ARGS...)> m_f; public: Foo( std::function<void(ARGS...)> f ) : m_f(f) {} void operator()(ARGS... args) const { m_f(args...); } }; In operator() I can access the args... easily with a recursive "peeling" function as described here http://www2.research.att.com/~bs/C++0xFAQ.html#variadic-templates My problem is: I want to access the types of the arguments of f, i.e. ARGS..., in the constructor.

are there function pointers in c#?

一世执手 提交于 2019-11-26 22:05:11
问题 I am trying to learn some c# coding and wondering if the c++ concept of function pointers is included in c#. I see there are such things as delegates. Are they the same concept? or do they differ on a more fundamental level? 回答1: Delegates are essentially function pointers, but with extra multicast capabilities built in. So you can assign several functions to the same delegate, and they will all be called in sequence when the delegate is called. Delegates also have built-in asynchronous

What does “void *(*)(void *)” mean in C++?

╄→гoц情女王★ 提交于 2019-11-26 21:53:07
问题 It's the parameter in pthread_create() . I think each part means: void * : The return value is a void pointer. (*) : It's a pointer to a function. (void *) : It takes an untyped pointer as a parameter. Is that correct? 回答1: Yes , it is the signature of a nameless function pointer that takes and returns void * . If it had a name (as in a variable) it would be: void *(*myFuncName)(void*) 来源: https://stackoverflow.com/questions/9371171/what-does-void-void-mean-in-c

Calling base class definition of virtual member function with function pointer

狂风中的少年 提交于 2019-11-26 21:48:23
问题 I want to call the base class implementation of a virtual function using a member function pointer. class Base { public: virtual void func() { cout << "base" << endl; } }; class Derived: public Base { public: void func() { cout << "derived" << endl; } void callFunc() { void (Base::*fp)() = &Base::func; (this->*fp)(); // Derived::func will be called. // In my application I store the pointer for later use, // so I can't simply do Base::func(). } }; In the code above the derived class