Since your
y1
doesn't access any member of the
test
then you may modify it to be
static
and write
#include <iostream>
using namespace std;
const void (*y2)(char i);
class test
{
static const void y1(char i)
{
cout << i << "\n";
}
public:
test();
};
test::test()
{
y2 = &y1;
}
int main()
{
test t;
y2('F');
}
The corresponding code for an ordinary (non static) method would be
#include <iostream>
using namespace std;
class test;
const void (test::*y2)(char i);
class test
{
const void y1(char i)
{
cout << i << "\n";
}
public:
test();
};
test::test()
{
y2 = &test::y1;
}
int main()
{
test t;
(t.*y2)('F');
}
Please note, in both cases the code, though working, is pretty meaningless.