您的位置:首页 > 其它

成员函数指针,静态成员函数指针,友元函数指针

2009-03-20 15:48 281 查看
#include <stdio.h>
class A
{
public:
A(int a) : m_a(a) { }
static void foo(void* param)
{
printf("static %s/n", __func__);
}
void foo()
{
printf("%s/n", __func__);
}
friend void foo(const A& a)
{
printf("friend %s, %d/n", __func__, a.m_a);
}
friend void foo1(const A& a);
private:
int m_a;
};

void foo(const A& a);
void foo1(const A& a)
{
printf("friend %s, %d/n", __func__, a.m_a);
}
int main(int argc, char* argv[])
{
void (*f1)(void*) = A::foo;
void (*f2)(void*) = &A::foo;
(*f1)(NULL);
(*f2)(NULL);
A a(1);
// void (A::*f3)() = A::foo;    // error
void (A::*f4)() = &A::foo;
(a.*f4)();
void (*f5)(const A& a) = &foo;
(*f5)(a);
void (*f6)(const A& a) = &foo1;
(*f6)(a);
return 0;
}


结果:

static foo
static foo
foo
friend foo, 1
friend foo1, 1

编译环境:

g++ (GCC) 4.1.2 20071124 (Red Hat 4.1.2-42)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐