0

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"
1
  • Please decide for either C or C++ Commented Mar 6, 2013 at 9:47

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I was about to ask about the possibility of using lambda functions with std::function myself. Do you know off hand whether this will work with VC++ or g++, and from which version? (I know that neither really support C++11, but some things do work in recent versions---I can use simple lambdas in VC++11, for example.)
@JamesKanze Builds fine with GCC 4.7.1. Also tested with clang version 3.3 (trunk 176337). Not tested yet with VC++.
@JamesKanze Just checked with VC++2012 Express, works fine there too. Can't say anything about 2010 though.
3

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;

1 Comment

That is not a valid declaration. Functions cannot return plain arrays.
1

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

Comments

1

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.)

Comments

1
    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];
  }

}

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.