指向靜態成員函式的指標

static 成員函式就像普通的 C / C++函式一樣,除了作用域:

  • 它在 class 裡面,所以它需要用類名裝飾它的名字;
  • 它有可訪問性,publicprotectedprivate

因此,如果你可以訪問 static 成員函式並正確裝飾它,那麼你可以像 class 之外的任何正常函式一樣指向函式:

typedef int Fn(int); // Fn is a type-of function that accepts an int and returns an int

// Note that MyFn() is of type 'Fn'
int MyFn(int i) { return 2*i; }

class Class {
public:
    // Note that Static() is of type 'Fn'
    static int Static(int i) { return 3*i; }
}; // Class

int main() {
    Fn *fn;    // fn is a pointer to a type-of Fn

    fn = &MyFn;          // Point to one function
    fn(3);               // Call it
    fn = &Class::Static; // Point to the other function
    fn(4);               // Call it
 } // main()