O'Reilly logo

Mastering C++ Programming by Jeganathan Swaminathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required