functional
Prerequisites:
以上文章必須看完,才能往下看。
大陸稱包裝器,台灣目前不知稱為什麼。
功能類似函式指標
include
#include <functional>
語法1
宣告function
function<函式傳回值類型(函式參數1類型 參數名1,函式參數2類型 參數名2, ...)> 變數名 = 函式名;
完整程式碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <functional>
using namespace std;
//函式定義
void print(int code, const string& msg) {
cout << "Error code = " << code << " , Msg = " << msg << endl;
}
int main() {
//宣告function
function<void(int, const string&)> func = print;
//呼叫函式
func(500, "Server error.");
return 0;
}
Error code = 500 , Msg = Server error.
語法2
可以把函式類型取別名
using FuncType = void(int, const string&);
宣告function時,在模板類型設成FuncType函式類型別名
function<FuncType> func = print;
完整程式碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <functional>
using namespace std;
//函式定義
void print(int code, const string& msg) {
cout << "Error code = " << code << " , Msg = " << msg << endl;
}
int main() {
using FuncType = void(int, const string&);
//宣告function
function<FuncType> func = print;
//呼叫函式
func(500, "Server error.");
return 0;
}
Error code = 500 , Msg = Server error.
類別靜態函式指標
語法
function<void(int, const string&)> func = Student::print;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <functional>
using namespace std;
class Student {
public:
static void print(int code, const string& msg) {
cout << "Error code = " << code << " , Msg = " << msg << endl;
}
};
int main() {
//宣告function
function<void(int, const string&)> func = Student::print;
//呼叫函式
func(500, "Server error.");
return 0;
}
物件函式
語法
function<函式傳回值類型(函式參數1類型,函式參數2類型, ...)> func = 物件名;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <functional>
using namespace std;
class Student {
public:
void operator()(int code, const string& msg) {
cout << "Error code = " << code << " , Msg = " << msg << endl;
}
};
int main() {
//建立物件
Student student;
//宣告function
function<void(int, const string&)> func = student;
//呼叫函式
func(500, "Server error.");
return 0;
}
lambda
程式碼1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <functional>
using namespace std;
int main() {
//lambda
auto print = [](int code, const string& msg) {
cout << "Error code = " << code << " , Msg = " << msg << endl;
};
//宣告function
function<void(int, const string&)> func = print;
//呼叫函式
func(500, "Server error.");
return 0;
}
程式碼2
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <functional>
using namespace std;
int main() {
//宣告function
function<void(int, const string&)> func = [](int code, const string& msg) {
cout << "Error code = " << code << " , Msg = " << msg << endl;
};
//呼叫函式
func(500, "Server error.");
return 0;
}
物件成員函式
宣告function
- function<參數類型1, …> 第1個參數要填入類別參考&
- 等於號(=)指派對映的函式,必須把記憶體位址傳進去,所以使用&取位址運算子+類別名+::範圍運算子+函式名
function<void(Student&,int, const string&)> func = &Student::print;
呼叫function
必須把物件代入第1個參數
func(student, 500, "Server error.");
完整程式碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <functional>
using namespace std;
class Student {
public:
void print(int code, const string& msg) {
cout << "Error code = " << code << " , Msg = " << msg << endl;
}
};
int main() {
//建立物件
Student student;
//宣告function
function<void(Student&,int, const string&)> func = &Student::print;
//呼叫函式
func(student, 500, "Server error.");
return 0;
}