Skip to content

Instantly share code, notes, and snippets.

@silv3rm00n
Created December 27, 2011 14:44
Show Gist options
  • Save silv3rm00n/1523864 to your computer and use it in GitHub Desktop.
Save silv3rm00n/1523864 to your computer and use it in GitHub Desktop.
/*
* 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