// C++ program to pass function as a
// pointer to any function
#include <iostream>
using namespace std;
// Function that add two numbers
int add(int x, int y)
{
return x + y;
}
// Function that multiplies two
// numbers
int multiply(int x, int y)
{
return x * y;
}
// Function that takes a pointer
// to a function
int invoke(int x, int y,
int (*func)(int, int))
{
return func(x, y);
}
// Driver Code
int main()
{
// Pass pointers to add & multiply
// function as required
cout << "Addition of 20 and 10 is ";
cout << invoke(20, 10, &add)
<< '
';
cout << "Multiplication of 20"
<< " and 10 is ";
cout << invoke(20, 10, &multiply)
<< '
';
return 0;
}
class MyClass
{
public:
MyClass();
// Note: No longer marked `static`, and only takes the actual argument
void Callback(int x);
private:
int private_x;
};
MyClass::MyClass()
{
using namespace std::placeholders; // for `_1`
private_x = 5;
handler->addHandler(std::bind(&MyClass::Callback, this, _1));
}
void MyClass::Callback(int x)
{
// No longer needs an explicit `instance` argument,
// as `this` is set up properly
cout << x + private_x << endl;
}
// program to print a text
#include <iostream>
using namespace std;
// display a number
void displayNum(int n1, float n2) {
cout << "The int number is " << n1;
cout << "The double number is " << n2;
}
int main() {
int num1 = 5;
double num2 = 5.5;
// calling the function
displayNum(num1, num2);
return 0;
}
// program to print a text
#include <iostream>
using namespace std;
// display a number
void displayNum(int n1, float n2) {
cout << "The int number is " << n1;
cout << "The double number is " << n2;
}
int main() {
int num1 = 5;
double num2 = 5.5;
// calling the function
displayNum(num1, num2);
return 0;
}