user1679802
user1679802

Reputation: 167

function call as array element

i have some difficulties understanding and implementing the following issue:

storing a function call in an array, i.e. having an array of doubles and a function that return a double....i would like to when calling e.g. an element of the array, lets say myArray[0], it should call the function myArray, that returns a double.

double myFunction(){return mydouble}

double myArray[3];
...
cout<<myArray[2]<<endl; <- should call myFunction and return "mydouble"

Upvotes: 0

Views: 206

Answers (6)

Alexey Frunze
Alexey Frunze

Reputation: 62048

How about something as simple as this?:

#include <iostream>
#include <map>

struct O
{
  std::map<size_t,double(*)()> m;
  double operator[](size_t index)
  {
    return m[index]();
  }
};

double mydouble = 1.25;

double myFunction()
{
  return mydouble;
}

int main()
{
  O myArray;
  myArray.m[2] = &myFunction;
  std::cout << myArray[2] << std::endl;
  return 0;
}

Output (ideone):

1.25

Upvotes: 1

Saurabh B
Saurabh B

Reputation: 165

    double myFunction()
{
    double mydouble = 1;
  return mydouble;
}

int main()
{
  double myArray[3] = {0,0,0};
  for(int i=0;i<3;i++)
  {
    myArray[i] = myFunction();
    cout << myArray[i];
  }

}

Upvotes: 1

Hitesh Menghani
Hitesh Menghani

Reputation: 987

Then you should have array of function pointer in order to achieve this:

typedef double(*MyArray)();

MyArray MA[3];

You can store functions with signature double function_name() into array,like:

MA[0] = MyFunction;

cout<< MA[0]()<<endl;

Upvotes: 3

James Kanze
James Kanze

Reputation: 153909

The solution is different in C and in C++, and also different in C++11 and in pre-C++11.

In all of these, you can have a simple array of pointers to functions:

double (*array[3])() = { &f1, &f2, &f3 };

To call a function:

std::cout << (*array[i])() << std::endl;

This is very limiting, since it means that the functions in question cannot depend on any other data. For this reason, it is usual in C to create a struct:

struct F
{
    double (*pf)( void* );
    void*    data;
};

F array[3] = { { &f1, NULL }, { &f2, someData }, { &f3, NULL } };

std::cout << (*array[i].pf)( &data ) << std::endl;

(There are, however, a lot of cases, particularly in numerical calculation, where this is overkill. It's mostly used by callbacks.)

In C++, you also have the option of defining an abstract base class, with a virtual function (which will be overridden in each derived class), and saving pointers to instances of various derived classes:

class F
{
public:
    virtual ~F() {}
    virtual double operator()() const = 0;
};

F const* array[3] = { &obj1, &obj2, &obj3 };

std::cout<< (*array[i])() << std::endl;

Finally, in C++11, there is a standard object std::function which encapsulates all of this. (You'll have to look this up yourself, however. Like most people, I don't yet have access to a compiler which fully supports C++11, so have been unable to practice this.)

Upvotes: 1

David Ranieri
David Ranieri

Reputation: 41017

In C you can do it in this way:

#include <stdio.h>

/* Functions */
static double fn_0(void){return 0.;}
static double fn_1(void){return 1.;}
static double fn_2(void){return 2.;}

int main(void)
{
    /* Declaration */
    double (*pfn[])(void) = {fn_0, fn_1, fn_2};
    int i;

    for (i = 0; i < 3; i++) {
        printf("%f\n", pfn[i]()); /* Usage */
    }
    return 0;
}

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409166

For C++ look up std::function, for C read more about function pointers.

Function pointers can be used in C++ as well, but are not as flexible as std::function.

C++ solution:

struct MyStruct
{
    double myStructFunction() { return 2.0; }
};

std::function<double()> myArray[3];

MyStruct myStructure;

myArray[0] = []() { return 1.0; };
myArray[1] = std::bind(&MyStruct::myStructFunction, myStructure);
myArray[2] = myFunction;

for (int i = 0; i < 3; i++)
    std::cout << "Calling `myArray[" << i << "]` : " << myArray[i]() << '\n';

For the assignment of myArray[0] look up lambda functions, and for the myArray[1] assignment look up std::bind.

Upvotes: 5

Related Questions