Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

functors in c++

//BISMILLAHIR RAHMANIR RAHIM

#include<bits/stdc++.h>
#define arr_size(x)   sizeof(x)/sizeof(x[0]);
#define el    '
'
using namespace std;


class myFunctor
{

private:
    int myObject;

public:

    myFunctor (int parameterVar = 0) // constructor
    {
        myObject = parameterVar;
    }

    /* the "operator" word is a keyword which indicates this function is an
       overloaded operator function. The () following this just tells the
       compiler that () is the operator being overloaded.  */

    int operator() (int myArgument)
    {
        return myObject + myArgument;
    }

};


int main()
{

    int array[5] = {1, 2, 3, 4, 5};

    transform(array, array + 5, array, myFunctor(100));

    int sz = arr_size(array)

     for (int i =0; i< sz; i++)
     {
         cout<<array[i]<<" "<<el;
     }

    return 0;

}

Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #functors
ADD COMMENT
Topic
Name
1+9 =