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

#include 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.

 

你可能感兴趣的:(C/C++程序员,Linux程序员)