using函式指標別名

Prerequisites:

以上文章必須看完,才能往下看。

using替函式指標取類型別名

與typedef一樣,都是替函式指標取類型別名

定義函式指標類型別名語法

using 類型別名 = 函式指標
using Func = void(int, const string&);

函式宣告

print函式與void(int, const string&)是相符

注意!以下的print後面是沒有()括號

Func print;

以上程式碼等同普通函式宣告

void print(int, const string&);

函式定義

所謂的函式定義就是有實作程式碼的函式

1
2
3
4
5
6
7
8
9
10
11
//Func類型別名
using Func = void(int, const string&);
//函式宣告
Func print;
int main() {
  return 0;
}
//函式定義
void print(int code, const string& msg) {
  cout << "Error code = " << code << " , Msg = " << msg << endl;
}
Error code = 500 , Msg = Server error.

函式指標

宣告函式指標,並指向函式

類型別名* 函式指標變數名 = 函式;
Func* func_pointer = print;

呼叫函式

函式指標變數名(參數)
func_pointer(500, "Server error.");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Func函式類型別名
using Func = void(int, const string&);
//函式宣告
Func print;
int main() {
  //宣告函式指標,指向print函式
  Func* func_pointer = print;
  //呼叫函式
  func_pointer(500, "Server error.");
  
  //宣告函式參考,指向print函式
  Func* func_ref = print;
  func_ref(400, "Not Found.");
  return 0;
}
//函式定義
void print(int code, const string& msg) {
  cout << "Error code = " << code << " , Msg = " << msg << endl;
}
Error code = 400 , Msg = Not Found.

函式參考

宣告函式參考,並指向函式

類型別名& 函式指標變數名 = 函式;
Func& func_ref = print;

呼叫函式

函式參考變數名(參數)
func_ref(500, "Server error.");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Func函式類型別名
using Func = void(int, const string&);
//函式宣告
Func print;
int main() {
  //宣告函式參考,指向print函式
  Func* func_ref = print;
  //呼叫函式
  func_ref(400, "Not Found.");
  return 0;
}
//函式定義
void print(int code, const string& msg) {
  cout << "Error code = " << code << " , Msg = " << msg << endl;
}
Error code = 400 , Msg = Not Found.

類別靜態函式指標

函式指標語法

加上類別名字以及範圍運算子::

void (*func_pointer)(int, const string&) = Student::print;
1
2
3
4
5
6
7
8
9
10
11
12
class Student {
public:
  static void print(int code, const string& msg) {
  cout << "Error code = " << code << " , Msg = " << msg << endl;
  }
};
int main() {
  //宣告函式指標
  void (*func_pointer)(int, const string&) = Student::print;
  func_pointer(500, "Server error.");
  return 0;
}
Error code = 500 , Msg = Server error.

使用using函式指標別名

加上類別名字以及範圍運算子::

Func *func_p = Student::print;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Student {
public:
  static void print(int code, const string& msg) {
  cout << "Error code = " << code << " , Msg = " << msg << endl;
  }
};
//Func函式類型別名
using Func = void(int, const string&);
int main() {
  //函式類型別名宣告函式指標
  Func *func_p = Student::print;
  func_p(404, "Page not Found.");
  return 0;
}
Error code = 404 , Msg = Page not Found.

物件成員函式指標

物件成員函式指標語法

  • 必須把記憶體位址傳進去,所以有使用&取位址運算子
  • 呼叫函式時,使用物件加點(.)運算子與指標運算子(*)
void(Student::* func_pointer)(int, const string&) = &Student::print;
  (student.*func_pointer)(500, "Server error.");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Student {
public:
  void print(int code, const string& msg) {
  cout << "Error code = " << code << " , Msg = " << msg << endl;
  }
};
int main() {
  //物件宣告
  Student student;
  //成員函式轉成函式指標
  void(Student::* func_pointer)(int, const string&) = &Student::print;
  //函式指標呼叫函式
  (student.*func_pointer)(500, "Server error.");
  return 0;
}
Error code = 500 , Msg = Server error.

使用using函式指標別名

  1. 函式指標類型別名
     using Func = void(Student::*)(int, const string&);
     
  2. 建立函式指標
     Func func_pointer = &Student::print;
     
  3. 呼叫函式指標
     (student.*func_pointer)(400,"Page not found.");
     

完整程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Student {
public:
  void print(int code, const string& msg) {
  cout << "Error code = " << code << " , Msg = " << msg << endl;
  }
};
//Func函式類型別名
using Func = void(Student::*)(int, const string&);
int main() {
  //建立物件
  Student student;
  //建立函式指標
  Func func_pointer = &Student::print;
  //呼叫函式指標
  (student.*func_pointer)(400,"Page not found.");
  return 0;
}
Error code = 400 , Msg = Page not found.

results matching ""

    No results matching ""