> (someclass->test->*funcptr)(2, "Lol");
Remove the * (someclass->test->funcptr)(2, "Lol");
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
Are you trying to declare a SomeClass member from inside of MyClass?
MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
You never declare 'funcptr', you just have it randomly appearing in main() referencing something. It also can't be declared in main() and then used as a type in your outside defined class.
MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
SomeClass::*funcptr doesn't exist, its not a SomeClass member. I believe it would be something more like:
typedef int (*fp)(int, char*);
class MyClass{
public:
fp *funcptr;
}
I cannot confirm that is correct, I need to oil up the rusting gears in my head. I generally don't use pointer functions unless I'm using a raw code catalyst of sorts. I'm sure someone will sort this out.
MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
I think this is what you were after, note that you need an instance of the class through which you make the call ( pB = new B; below)
#include <iostream>
struct B;
struct A
{
B * pB;
A();
~A();
void func(int i, const char * p)
{
std::cout << i << std::endl << p << std::endl;
}
};
struct B
{
void (A::* funcptr)(int, const char *);
};
A::A()
{
// a B is needed
pB = new B;
}
A::~A()
{
delete pB;
}
int main()
{
A * pA = new A;
pA->pB->funcptr = &A::func;
(pA->*(pA->pB->funcptr))(2, "Lol");
delete pA;
return 0;
}
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395