首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > PB >

把成员函数以指针形式传递有关问题请问~

2012-05-13 
把成员函数以指针形式传递问题请教~~~~~C/C++ code#includestdio.htypedef int (*printfun)(char *str)

把成员函数以指针形式传递问题请教~~~~~

C/C++ code
#include<stdio.h>typedef int (*printfun)(char *str);class A{public:    void ap(printfun pf)    {        pf("aaaaa");        printf("calss A\n");    }};class B{public:    int print(char *str)    {        printf("class B : %s\n",str);    }    void bp()    {        A a;        a.ap(print);    }};void main(){    B b;    b.bp();}

vc6下编译错误:
C:\Documents and Settings\Administrator\test.cpp(27) : error C2664: 'ap' : cannot convert parameter 1 from 'int (char *)' to 'int (__cdecl *)(char *)'
  None of the functions with this name in scope match the target type

[解决办法]
typedef int (*B::printfun)(char *str);

成员函数指针和普通的函数指针是不同的。
要区分开来


[解决办法]
print函数是B的成员函数 在A中用this->printfunc调用不了的 你这个已经没意义了 除非得到B对象的地址
[解决办法]
http://blog.csdn.net/wohaaitinciu/article/details/7436006
[解决办法]
探讨
C/C++ code

#include<stdio.h>
class B;
typedef int (B::*printfun)(char *str);

class A
{
public:
void ap(printfun pf)
{
//pf("aaaaa");
printf("calss A\n");
}
……

热点排行