Created
December 27, 2011 14:44
-
-
Save silv3rm00n/1523864 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Function pointers and classes | |
* The program works correctly by printing 5 with the "Dont want to do this" lines | |
* Need to get the program working without the "Dont want to do this" lines | |
* */ | |
#include<stdio.h> | |
class A | |
{ | |
public: | |
void trial(); | |
void log(int a) | |
{ | |
printf("%d" , a); | |
} | |
}; | |
class B | |
{ | |
public: | |
void (*logger)(int); | |
//void (A::*logger)(int); -- Dont want to do this | |
void set_handler(void (*functocall)(int)) | |
//void set_handler(void (A::*functocall)(int)) -- Dont want to do this | |
{ | |
logger = functocall; | |
} | |
void test() | |
{ | |
(*logger)(5); //Giving strange output - 4196190 , it should give 5 instead | |
//(*((A*)this).*logger)(5); -- Dont want to do this | |
} | |
}; | |
void A::trial() | |
{ | |
B bb; | |
bb.set_handler( (void (*)(int)) &A::log); | |
bb.test(); | |
} | |
int main() | |
{ | |
A aa; | |
aa.trial(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment