博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

寻路

摄心为纲, 断惑为要, 常行惭愧, 常观己心.
   linfengfeiye.cublog.cn
关于作者  
姓名:秒振
职业:计算机
年龄:28
位置:
个性介绍:

我的分类  




函数指针作为形参
/***一般函数情况****/
#include <iostream>
#include <string>
using namespace std;
void function1(string& x)
{
     cout<<x;
}
void function2(string& x)
{
     cout<<x<<endl;
}
void Test(void(*p)(string& y),string& z)
{
    cout<<"this is a test"<<endl;
    p(z);
 }
int main()
{
    string x("welcome!");
    Test(function1,x);
    system("pause");
}
/******成员函数情况********/
#include  <iostream>
using namespace std;

class B 

public :
    double funB(double a){return a;}; 
};

class C 

public :
    double funC(double a){return a;}; 
};

class A

public :
template  <typename T>
double funA(double (T::*func)(double),T t,double a){return (t.*func)(a);};
};

int main() 
{  
A a;
B b;
double dParam = 3.0;

cout < <a.funA(&B::funB,b,dParam) < <endl; 

return 0 ; 
}
//或者
class base

vitual double funA()=0; \\纯虚函数 (如果你在b类 C类等其它类的实现功能不一样的话
} ;
也可以这样:
class base

 
double funB(double){//如果你在其它类中的功能都是一样的话
//函数体

} ;

class a:public base
{
.
.
.
};
class b:public base
{
.
.
.
};

 发表于: 2008-05-12,修改于: 2008-05-12 14:25 已浏览81次,有评论0条 推荐 投诉

  网友评论

  发表评论



Copyright © 2001-2006 ChinaUnix.net All Rights Reserved

感谢所有关心和支持过ChinaUnix的朋友们
页面生成时间:0.00876

京ICP证041476号