65,210
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
class B;
class A
{
public:
A(void (B::*ptr_fun)())
{
_ptr_fun = ptr_fun;
}
void fun()
{
_ptr_fun(); ------编译期间错误
}
private:
void (B::*_ptr_fun)();
};
class B
{
public:
void fun()
{
obj = new A(&B::fun);
std::cout << "B::fun()" <<std::endl;
obj->fun();
}
private:
A* obj;
};
void main()
{
B b;
b.fun();
system("pause");
}
#include<iostream>
class B;
class A
{
public:
A(void (B::*ptr_fun)(int a))
{
_ptr_fun = ptr_fun;
}
void fun()
{
(((B*)NULL)->*_ptr_fun)(10); // same as ((*(B*)NULL).*_ptr_fun)(10);
}
private:
void (B::*_ptr_fun)(int a);
};
class B
{
public:
void fun(int a)
{
std::cout << "B::fun() " << a <<std::endl;
}
void test()
{
obj = new A(&B::fun);
obj->fun();
delete obj;
}
private:
A* obj;
};
void main()
{
B b;
b.test();
system("pause");
}
#include<iostream>
class B;
class A
{
public:
A(void (B::*ptr_fun)(int a))
{
_ptr_fun = ptr_fun;
}
void fun()
{
_ptr_fun(10); ------------还是不行
}
private:
void (B::*_ptr_fun)(int a);
};
class B
{
public:
void fun(int a)
{
obj = new A(&B::fun);
std::cout << "B::fun()" <<std::endl;
obj->fun();
}
private:
A* obj;
};
void main()
{
B b;
b.fun(10);
system("pause");
}