function-pointers

How to create a container that holds different types of function pointers in C++?

南楼画角 提交于 2019-12-02 04:53:22
I'm doing a linear genetic programming project, where programs are bred and evolved by means of natural evolution mechanisms. Their "DNA" is basically a container (I've used arrays and vectors successfully) which contain function pointers to a set of functions available. Now, for simple problems, such as mathematical problems, I could use one type-defined function pointer which could point to functions that all return a double and all take as parameters two doubles. Unfortunately this is not very practical. I need to be able to have a container which can have different sorts of function

Why can I still call a function from a pointer class after I delete it? [duplicate]

不羁的心 提交于 2019-12-02 04:10:29
Possible Duplicate: What will happen when I call a member function on a NULL object pointer? #include <iostream> #include <string> using namespace std; class myClass{ private: int *x, *y, *z; public: myClass(); ~myClass(); void display(); void math(int,int,int); }; void myClass::math(int x,int y,int z){ this->x = new int; this->y = new int; this->z = new int; *this->x = x; *this->y = y; *this->z = z; cout << "result: " << (x*y)+z << endl; } myClass::~myClass(){ delete x; delete y; delete z; } void myClass::display(){ cout << x << y << z << endl; } myClass::myClass(){ x=0; y=0; z=0; } int main(

Why do function objects evaluate to True in python?

旧巷老猫 提交于 2019-12-02 03:57:29
In python it is valid to make a construction like: def a(): return 0 if a: print "Function object was considered True" else: print "Function object was considered False" I wish to ask what is the logic that a function pointer is evaluated to True. Why was this kind of construction inserted in the language? Martijn Pieters A lot of things evaluate to True in Python. From the documentation on Boolean operators : In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False , None , numeric zero of all

Explain typedef for function used in qsort library

跟風遠走 提交于 2019-12-02 03:54:38
问题 I am using qsort library function to sort an array of structure elements, while searching on the Internet I found a resource: INFO: Sorting Structures with the C qsort() Function @ support.microsoft. I understand that qsort function requires to be typecast by generic pointers. However I am not able to get this line: typedef int (*compfn) (const void*, const void*); Which has been declared, and its subsequent call: qsort((void *) &array, // Beginning address of array 10, // Number of elements

How to initialize array of pointers to functions?

江枫思渺然 提交于 2019-12-02 03:25:50
I have following code: typedef int (*t_Function) (int x); t_Function Functions[MAX_FUNCTIONS]; int f(int x) { return 0; } But I cannot initialize it properly. If I add following line: Functions[0] = f; then compiler generates following error: prog.c:217: warning: data definition has no type or storage class prog.c:217: error: conflicting types for Functions How to initialize this array of pointers to functions? You should either do it inside a function, where Functions[0] = f; works fine, or with an array initializer: t_Function Functions[MAX_FUNCTIONS] = {f}; For this to work, f (and all

How to call a function using pointer-to-member-function

∥☆過路亽.° 提交于 2019-12-02 03:10:06
I have a class: class A { void test_func_0(int); void run(); typedef void(A::*test_func_t)(int); struct test_case_t{ test_func_t test_func; } test_case[100]; }; Now I want to call test_func() inside run(): void A::run() { test_case[0].test_func = &test_func_0; test_case[0].*(test_func)(1); } The last line of my code, doesn't work(compile error), no matter what combination I try. Use this: void A::run() { test_case[0].test_func = &A::test_func_0; (this->*(test_case[0].test_func))(1); } Notice that you had 2 errors. The first one was how you formed the member-function-pointer. Note that the only

What is the correct argument to pthread_create

跟風遠走 提交于 2019-12-02 03:08:11
问题 I have seen the documentation of pthread_create In the example at the bottom they are using: pthread_create(&tinfo[tnum].thread_id, &attr, &thread_start, &tinfo[tnum]); &thread_start - with & but in other examples I have seen online they were not using the & : pthread_create(&tinfo[tnum].thread_id, &attr, thread_start, &tinfo[tnum]); I have also tested and it works without & . But which is the correct way? 回答1: Short answer: both are correct. The signature of pthread_create is: int pthread

Function returning a pointer to an int array

孤者浪人 提交于 2019-12-02 00:05:26
问题 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

pointer to member function

依然范特西╮ 提交于 2019-12-01 23:46:19
问题 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

Why do function pointers all have the same value?

微笑、不失礼 提交于 2019-12-01 22:50:32
For example: using namespace std; #include <iostream> void funcOne() { } void funcTwo( int x ) { } int main() { void (*ptrOne)() = funcOne; cout << ptrOne << endl; //prints 1 void (*ptrTwo)( int x ) = funcTwo; cout << ptrTwo << endl; //prints 1 int (*ptrMain)() = main; cout << ptrMain << endl; //prints 1 } Does anyone know the reasoning behind this? At first I thought it was because the functions don't exist in memory since I never call on them, and thus they never get added to the stack. But even the value of a pointer to the main function prints out 1. Function pointers do not implicitly