- Mastering C++ Programming
- Jeganathan Swaminathan
- 156字
- 2021-07-02 18:28:46
The std::invoke( ) method
The std::invoke() method can be used to call functions, function pointers, and member pointers with the same syntax:
#include <iostream>
#include <functional>
using namespace std;
void globalFunction( ) {
cout << "globalFunction ..." << endl;
}
class MyClass {
public:
void memberFunction ( int data ) {
std::cout << "\nMyClass memberFunction ..." << std::endl;
}
static void staticFunction ( int data ) {
std::cout << "MyClass staticFunction ..." << std::endl;
}
};
int main ( ) {
MyClass obj;
std::invoke ( &MyClass::memberFunction, obj, 100 );
std::invoke ( &MyClass::staticFunction, 200 );
std::invoke ( globalFunction );
return 0;
}
The preceding code can be compiled and the output can be viewed with the following commands:
g++-7 main.cpp -std=c++17
./a.out
The output of the preceding program is as follows:
MyClass memberFunction ...
MyClass staticFunction ...
globalFunction ...
The std::invoke( ) method is a template function that helps you seamlessly invoke callable objects, both built-in and user-defined.
推薦閱讀
- UI設計基礎培訓教程
- Getting Started with Citrix XenApp? 7.6
- 信息可視化的藝術:信息可視化在英國
- 深入淺出Prometheus:原理、應用、源碼與拓展詳解
- C語言程序設計實踐教程
- 精通Python自然語言處理
- Mastering Rust
- Linux操作系統基礎案例教程
- 可解釋機器學習:模型、方法與實踐
- 軟件測試技術指南
- Java程序員面試筆試寶典(第2版)
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- WebStorm Essentials
- Advanced Python Programming
- Modular Programming with JavaScript