1.函数指针
#include <iostream>
using namespace std;
void hello(){
cout<<"abc"<<endl;
}
int abc(int x){
return x+1;
}
typedef void (*pHello)();.........这一行就是typedef的函数指针funcpointer,其实他们就是替代关系,看懂了就
int main(int argc, char *argv[]) 好说了,没看懂就觉得很深奥,*pHello就是代替了hello,那么这个指针就应该
{ 指向hello,*phello=&hello;
//int (*fAbc)(int axc);
//fAbc=&abc;
//cout<<(*fAbc)(1)<<endl;
void (*pf)();
pf = &hello;
//pf = hello;
(*pf)();
//pf();
pHello p = &hello;
(*p)();
return 0;
}
/*
typedef int (*pF)(int); ............这个也是函数指针,这个int必须写上去标明我这里有一个参数。
pF y; 其余和上面差不多。
y=&abc;
cout<<(*y)(1)<<endl;
*/
2.成员变量指针
#include <iostream>
using namespace std;
class A{
public:
int x;
};
void func(A obj, int A::* p){ //MemberPointer p
cout<<obj.*p<<endl;
}
void func2(A * obj, int A::* p){
cout<<obj->*p<<endl;
}
template <class T> ...........c++函数模版,根据T类型来决定函数的类型避免重复编程
void func3(T * obj, int T::* p){
cout<<obj->*p<<endl;
}
typedef int A::* MemberPointer;.........成员变量指针 替代关系
int main(int argc, char *argv[])
{