函数传递问题

[复制链接]
 楼主| sinadz 发表于 2012-4-19 18:15 | 显示全部楼层 |阅读模式
#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
yybj 发表于 2012-4-19 18:34 | 显示全部楼层
typedef int (*B::printfun)(char *str);

成员函数指针和普通的函数指针是不同的。
要区分开来
xsgy123 发表于 2012-4-19 20:27 | 显示全部楼层
print函数是B的成员函数 在A中用this->printfunc调用不了的 你这个已经没意义了 除非得到B对象的地址
您需要登录后才可以回帖 登录 | 注册

本版积分规则

304

主题

2313

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部

304

主题

2313

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部