Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to pass function as a parameter in c++

#include <bits/stdc++.h> 
using namespace std;

int add(int x, int y){return x+y;}
int sub(int x, int y){return x-y;}
int operation (int x, int y,int (*function)(int,int)){return function(x,y);}
int operation2(int x, int y,std::function<int(int, int)> function){return function(x,y);}

int main()
{
    std::cout <<"Values 1 & 3. Pointer function: Add:"<<operation (1,3,&add)<<" Sub:"<<operation (1,3,&sub) << std::endl;
    std::cout <<"Values 1 & 3. std::function   : Add:"<<operation2(1,3,&add)<<" Sub:"<<operation2(1,3,&sub) << std::endl;
}
Comment

take a function as an argument in c++

// 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;
}
Comment

c++ pass function as argument

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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: minimum or maximum in array c++ 
Cpp :: Program to find GCD or HCF of two numbers c++ 
Cpp :: how to create a struct in c++ 
Cpp :: hashset in cpp 
Cpp :: abs in c++ used for 
Cpp :: assign value to a pointer 
Cpp :: pointer to pointer c++ 
Cpp :: memset c++ 
Cpp :: operator overloading c++ 
Cpp :: error in c++ 
Cpp :: data type c++ 
Cpp :: pragma HLS bracets 
Cpp :: C/C++ loop for 
Cpp :: c++ include difference between quotes and brackets 
Cpp :: function for reversing an array c++ stl 
Cpp :: lap trinh file explorer c++ 
Cpp :: Vaccine Dates codechef solution in c++ 
Cpp :: full pyramid in c++ 
Cpp :: strcmp in c++ header file 
Cpp :: forkortelse for intet 
Cpp :: how to open file without override c++ 
Cpp :: coin change top-down 
Cpp :: TCA9548 I2CScanner Arduino 
Cpp :: ue4 c++ enum variable declaration 
Cpp :: find node from pos linkedlist c++ 
Cpp :: C++ Converting Celsius to Kelvin 
Cpp :: Imports the elements in the array c++ 
Cpp :: c++ str 
Cpp :: delete an dynamic array 
Cpp :: export gcc g++ 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =