function-pointers

Private member function that takes a pointer to a private member in the same class

情到浓时终转凉″ 提交于 2019-12-01 22:45:21
问题 How can I do this? (The following code does NOT work, but I hope it explains the idea.) class MyClass { .... private: int ToBeCalled(int a, char* b); typedef (MyClass::*FuncSig)(int a, char* b); int Caller(FuncSig *func, char* some_string); } I want to call Caller in some way like: Caller(ToBeCalled, "stuff") and have Caller call ToBeCalled with whatever parameters it feels needs passing. If at all possible I want to keep everything encapsulated in the private part of my class. In reality, I

Function returning a pointer to an int array

若如初见. 提交于 2019-12-01 22:12:44
I am learning C++ from Primer 5th edition and I am at Returning a Pointer to an Array. The declaration of this function is: int (*func(int i))[10]; and it's expected to return a pointer to an array. I wrote code that does this: #include <iostream> #include <string> using namespace std; int *func(){ static int a[]={1,2,3}; return a; } int main (){ int *p=func(); for(int i=0;i!=3;++i){ cout<<*(p+i); } } And it is working. But I want to know the difference between what I made here and int (*func(int i))[10]; How I can make this function call work, because in the book, there isn't any concrete

Bubble sort universal implementation in C

那年仲夏 提交于 2019-12-01 21:41:59
I'm trying to make an universal bubble sort function. It allow to user to write its own compare and swap function. I implemented a swap and compare function for int type, but when I run the code for next array: {3, 5, 8, 9, 1, 2, 4, 7, 6, 0} , I get: 0 0 84214528 2312 1 2 4 7 6 0. Why this is happening? #include <stdio.h> #include <stdlib.h> #define true 1 #define false 0 int compInt(void *a, void *b) // FUNCTION FOR COMPARE INT { if (*(int*)(a) > *(int*)(b)) { return false; } // IF FIRST INT > SECOND INT (WRONG ORDER) RETURN FALSE return true; // RIGHT ORDER -> RETURN TRUE } I think the

Private member function that takes a pointer to a private member in the same class

最后都变了- 提交于 2019-12-01 21:24:49
How can I do this? (The following code does NOT work, but I hope it explains the idea.) class MyClass { .... private: int ToBeCalled(int a, char* b); typedef (MyClass::*FuncSig)(int a, char* b); int Caller(FuncSig *func, char* some_string); } I want to call Caller in some way like: Caller(ToBeCalled, "stuff") and have Caller call ToBeCalled with whatever parameters it feels needs passing. If at all possible I want to keep everything encapsulated in the private part of my class. In reality, I'd have about 50 functions like ToBeCalled , so I can't see a way to avoid this. Thanks for any

pointer to member function

▼魔方 西西 提交于 2019-12-01 20:24:20
I am trying to generalize the functions filterX() and filterY() in the following class Table to function filter() . The functions filterX() and filterY() only differ in the function they call inside the procedure. While filterX() calls getX() , filterY() calls getY() . #include <iostream> #include <string> #include <vector> using namespace std; class Row { public: void add(string x, string y, int val); string getX() const { return d_x; } string getY() const { return d_y; } int getVal() const { return d_val; } private: string d_x; string d_y; int d_val; }; class Table { public: void add(string

Function pointers working as closures in C++

匆匆过客 提交于 2019-12-01 19:08:18
Is there a way in C++ to effectively create a closure which will be a function pointer? I am using the Gnu Scientific Library and I have to create a gsl_function . This function needs to effectively "close" a couple of parameters available when I create it. Is there a nice trick to create a closure so that I don't have to pass all of them as params in the gsl_function structure? If not, should I just pass in a pointer to an array containing these parameters? EDIT I have tried to use boost::bind like this: #include <gsl/gsl_integration.h> #include <boost/bind.hpp> #include "bondpricecalculator

Is it possible to replace a method at runtime?

本秂侑毒 提交于 2019-12-01 18:35:26
I want to make a plugin system with the capability to override a method at runtime. Some answers say function pointers, but how about a defined function or class? Like this: class foo { public: bar(int foobar); } Is there a way to get function pointer for that, or replace it? BTW, hooking is not considered an answer because it's very platform specific and dangerous. Runtime function "replacement" can be achieved with one of several techniques: Polymorphism Standard library facilities such as std::function Third party libraries or platform specific techniques to translate or dispatch function

Writing a function pointer in c

吃可爱长大的小学妹 提交于 2019-12-01 18:07:06
I was recently reading a code, and found that a function pointer is written as : int (*fn_pointer ( this_args ))( this_args ) I usually encounter a function pointer like this : return_type (*fn_pointer ) (arguments); Similar thing is discussed here : // this is a function called functionFactory which receives parameter n // and returns a pointer to another function which receives two ints // and it returns another int int (*functionFactory(int n))(int, int) { printf("Got parameter %d", n); int (*functionPtr)(int,int) = &addInt; return functionPtr; } Can somebody tell me what is the difference

Call a raw address from Rust

穿精又带淫゛_ 提交于 2019-12-01 17:56:27
I am writing an OS in Rust and need to directly call into a virtual address that I'm calculating (of type u32 ). I expected this to be relatively simple: let code = virtual_address as (extern "C" fn ()); (code)(); However, this complains that the cast is non-primitive. It suggests I use the From trait, but I don't see how this could help (although I am relatively new to Rust and so could be missing something). error[E0605]: non-primitive cast: `u32` as `extern "C" fn()` --> src/main.rs:3:16 | 3 | let code = virtual_address as (extern "C" fn ()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note

Expression SFINAE to overload on type of passed function pointer

爷,独闯天下 提交于 2019-12-01 17:53:22
In this example a function is passed to an implicitly instantiated function template. // Function that will be passed as argument int foo() { return 0; } // Function template to call passed function template<typename F> int call(F f) { return f(); } template<typename F, typename A> int call(F f, A a) { return f(a); } int a = call(foo); We can break this code by adding an overload for foo() . int foo(int i) { return 0; } The name " foo " is now ambiguous and the example will no longer compile. This can be made to compile by explicitly providing function pointer type info. int (*func_takes_void)