相关讨论
看见一本书上用c语言写函数的时候用到函数指针,可能原来学的c语言太浅,不知道c中是否可以用函数指针,若可以那么它如何传递参数呢?
请问,c语言中是否提供通过函数名字符串获得函数指针的功能?
如果提供的话,通过指针调用函数可以传参数吗?
#include
//#includector>
#include<cmath>
using namespace std;
class A{
private:
typedef void (*p)(int i);
p pp;
public:
void print(int i){
cout<pp=print;
}
};
int main()
{
}
这个程序在Vc6.0下有一个错误。麻烦知道不啬告诉我,先谢谢了
by
ztj111
-
C/C++
-
2006-11-17 09:29:08 阅读(2465) 回复(16)
对于函数指针,格式的声明必须和实际指向的函数参数和返回值类型相对应;
那么能否根据运行时的信息动态声明一个函数指针,或者有否等价的方法?
例如:程序运行时从dll动态联结库中得到一个函数声明的格式int f1(int, int),然后可否声明一个与之对应的函数指针int (*fp)(int, int)? 或者有其他可替代的方法?
class TApp1;
typedef void (TApp1::*MEMBERFUNcTION)();
class TApp1
{
MEMBERFUNcTION Test;
void Fun1()
{
...
}
void Fun2()
{
...
}
void callFun()
{
//调用test所指向的函数指针,如果Test指向Fun1,调用Fun1函数,如果test指向Fun2,调用Fun2函数
//其它地方都调试通过,此处不知怎么写代码,希望各位指点????????谢谢!!!!
(this->;*Test)();//这样就实现...
在c中函数指针定义
类型 (*Function)(参数)
如:
int (*function)();
int f1()
{
return 0;
}
main()
{
function=f1;
(*function)();
}
但在c++类中怎样实现呢?请指教!
by
zengok
-
C/C++
-
2004-04-22 17:36:05 阅读(3077) 回复(17)
#include
int main (int argc, char * argv[])
{
int (*p)(int a,int b);
int Max(int c, int d);
p = &Max;
int x,y;
x=200,y=300;
printf("%d\n",(*p)(x,y));//1
printf("%d\n",p(x,y));//2
return 0;
}
int Max(int c, int d)
{
if(c>d)
return c;
else
return d;
}
1和2都正确。。。为什么这样?
最近编程用到了 c++ 成员函数指针之间的类型转换, 我看了一些资料,c++ 成员函数指针实际上是一个结构,而标准好像没有规定如何具体实现成员函数指针,不同的编译器有不同的实现。“在c++的标准化的过程中,在涉及继承的类的成员函数指针时,对于将成员函数指针转化为基类的成员函数指针还是转化为子类成员函数指针的问题和是否可以将一个类的成员函数指针转化为另一个不相关的类的成员函数指针的问题,人们曾有过很激烈的争论。然...
[code]
#include
using namespace std;
class A;
typedef void (A::*pAFun)(void); //定义类成员函数指针
class A{
public:
void fun(){
cout<<"A::fun run"<class B
{
protected:
A *_a;
pAFun _callback;
public:
B():_callback(NULL), _a(NULL)
{};
void setOnDo(pAFun _pf_callback, A * pA)
{
_callback = _pf_callback;
_a = pA;...