PDA

View Full Version : قرار دادن آدرس یک تابع در یک متغیر



abtin256
شنبه 29 آبان 1389, 01:18 صبح
با سلام من یکمی گیج شدم میخواهم آدرس یک تابع رو در یک متغیر قرار بدم اما نمیدونم اصول اون چطوری یا میخواهم محتویات یک متغیر رو بعنوان آدرس به یک اشاره گر تابع بدهم منون میشم منو راهنمایی کنید یا یک رفرنس که اینه توش باشه معرفی کنید:عصبانی++::عصبانی++:

sh4mid
شنبه 29 آبان 1389, 10:19 صبح
The Function Pointer Tutorials (http://www.newty.de/fpt/fpt.html)

r00tkit
شنبه 29 آبان 1389, 10:50 صبح
#include <cstdio>
using std::printf;

// Define the type func as a pointer to a void function with no parameters
typedef void (*func)();


void Test1() {
printf("nada1\n");
}

void Test2() {
printf("nada2\n");
}

func getFunction(int type) {
if(type==1)
return &Test1;
if(type==2)
return &Test2;
else
return 0;
}

int main() {
func p = 0;

p = getFunction(1);
(*p)();

p = getFunction(2);
(*p)();
}