It's my first time to use functor. So hope can get some guide here. My program need to call different function depend on input. so i thinking to use functor to do that.

e.g.
main.cpp

#include "functor.h"
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
//do get input and switch case to call ex_functor with different functor parameter
//
//e.g. one of the case is this ex_functor
    ex_functor( ret1, 5, 2);

    cin.get();
    return EXIT_SUCCESS;
}

functor.h

#ifndef FUNCTOR_H_INCLUDED
#define FUNCTOR_H_INCLUDED

int ret1 (int a, int b);
int ret2 (int a, int b);
int ret3 (int a, int b);
int ex_functor (int (*functor)(int a, int b), int a, int b);

#endif // FUNCTOR_H_INCLUDED

functor.cpp

#include <cstdlib>
#include <iostream>
#include "functor.h"

using namespace std;

int ret1 (int a, int b)
{
    cout<<a+b;
    return a+b;
}
int ret2 (int a, int b)
{
    cout<<a-b;
    return a-b;
}
int ret3 (int a, int b)
{
    cout<<a*b;
    return a*b;
}

int ex_functor (int (*functor)(int a, int b), int a, int b)
{
    (*functor)( a,  b);
    return 0;
}

I'm using code::blocks10.05, the error is undefine reference to 'ret1(int, int)' at main.cpp line 9

haha... sry, i solved myself already. It is because of functor.cpp or functor.h is already existed in microsoft bin files. So just change the files name then the error solved :p

Here is my updated code :D
functorrs.cpp

#include <cstdlib>
#include <iostream>
#include "functorrs.h"

using namespace std;

/* version 1: create functor array and global variable instead of parameter)
int a;
int b;

void storeAB (int x, int y)
{
    a=x;
    b=y;
}

void ret1 ()
{
    cout<<a+b;
//    return a+b;
}
void ret2 ()
{
    cout<<a-b;
//    return a-b;
}
void ret3 ()
{
    cout<<a*b;
//    return a*b;
}

void (*fp[]) () =
{
    ret1, ret2, ret3
};

void ex_functor (int mode)
{
//    (*functor)( a,  b);
    (*fp[mode]) ();
}
*/

/* version 2: create functor array and pass value through parameter)
int ret1 (int a, int b)
{
    cout<<a+b;
    return a+b;
}
int ret2 (int a, int b)
{
    cout<<a-b;
    return a-b;
}
int ret3 (int a, int b)
{
    cout<<a*b;
    return a*b;
}

int (*fp[]) (int a, int b) =
{
    ret1, ret2, ret3
};

void ex_functor (int mode, int a, int b)
{
//    (*functor)( a,  b);
    (*fp[mode]) (a, b);
}
*/

///* version 3: pass functor as parameter and pass functor parameter also
int ret1 (int a, int b)
{
    cout<<a+b;
    return a+b;
}
int ret2 (int a, int b)
{
    cout<<a-b;
    return a-b;
}
int ret3 (int a, int b)
{
    cout<<a*b;
    return a*b;
}

void ex_functor (int (*fp) (int a, int b), int a, int b)
{
//    (*functor)( a,  b);
    (*fp) (a, b);
}
//*/

functorrs.h

#ifndef FUNCTOR_H_INCLUDED
#define FUNCTOR_H_INCLUDED

/* version 1: create functor array and global variable instead of parameter)
void storeAB (int x, int y);
void ret1 ();
void ret2 ();
void ret3 ();
void ex_functor (int mode);
//int ex_functor (int (*functor)(int a, int b), int a, int b);
*/

/* version 2: create functor array and pass value through parameter)
int ret1 (int a, int b);
int ret2 (int a, int b);
int ret3 (int a, int b);
void ex_functor (int mode, int a, int b);
*/

int ret1 (int a, int b);
int ret2 (int a, int b);
int ret3 (int a, int b);
void ex_functor (int (*fp) (int a, int b), int a, int b);

#endif // FUNCTOR_H_INCLUDED

Awesome mate :) just mark the thread as solved please?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.