-1

I want to use for example this array of pointers to functions, without using STL.

That array is an array of pointers that I call functions OptionA, OptionB and so on.

int(*Functions[4])();
Functions[0] = OpionA;
Functions[1] = OptionB;
Functions[2] = OptionC;
Functions[0] = Exit;

Now if I write inside the function where I have my array

Functions[0];

I want to have called function 'OptionA' where it has been defined before for example like this:

int OptionA()
{
    cout << "OPTION A";
    _getch();
    return 0;
}

Is it possible to do this without STL? If not, I would like to know how to do it with STL.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • Much easier if you use `std::array` or `std::vector` rather than C-style arrays. – Jesper Juhl Mar 17 '18 at 15:56
  • 4
    Why do you not want to use STL? Your example is also entirely unclear on what you want to do – Passer By Mar 17 '18 at 15:57
  • So, can you show me an example how to do this? I have never used STL, maybe that's why – Artur Szczypta Mar 17 '18 at 15:59
  • 1
    An array of pointers to functions (function pointers) or an array of pointers to something else? – Galik Mar 17 '18 at 16:14
  • You might want to clarify your question. As it looks now, it's hard to guess what you really want to achieve. Maybe you can post your unsuccessful attempt to do whatever you need to do? Maybe you can post a wider piece of your code, with a place where you don't know what to put? Maybe explain better what you are really trying to do? You can [edit] your question to clarify it. – anatolyg Mar 17 '18 at 16:32
  • you must either create a struct with fixed number of elements, and pass that; a struct with a linked list in it, and pass that. a struct with lpVtbl(optionally) and data(allocated) and pass that and have the function free the data(destroy the structure when done); or an allocated structure that the function agrees to deallocate when done using it. – Dmytro Mar 17 '18 at 16:42
  • I haved edited my question, I hope now is more clarify. – Artur Szczypta Mar 17 '18 at 18:47
  • The title of the question was incorrect and confusing. I have edited the question to make it clearer, given the additional info. – anatolyg Mar 19 '18 at 13:34

4 Answers4

3

You can create and pass arrays of function pointers like any other types. It's easiest if you have a type alias (my example leverages using, but typedef will also work).

#include <iostream>

using Function = int (*)(int, int);

int add(int a, int b) {
    return a + b;
}

int sub(int a, int b) {
    return a - b;
}

void do_stuff(int a, int b, Function * fns, int cnt) {
    for(auto i = 0; i < cnt; ++i) {
        std::cout << "Result " << i << " = " << fns[i](a, b) << '\n';
    }
}

int main() {
    Function fns[2] = { add, sub };
    do_stuff(10, 7, fns, 2);
    return 0;
}

Output:

Result 0 = 17
Result 1 = 3
Stephen Newell
  • 7,330
  • 1
  • 24
  • 28
  • I'll echo what some of the comments on the questions have brought up: you should be using the stl if possible (`array` or `vector`), but this answers your specific question. – Stephen Newell Mar 17 '18 at 16:18
0

I think that what you are looking for is
How to initialize a vector of pointers

Once your vector is initialize you can send it to a function like a normal data type. Example:

std::vector<int*> array_of_pointers{ new int(0), new int(1), new int(17) };
function(array_of_pointers);

In the declaration of the function

void function(std::vector<int*> array_of_pointers);

I hope this answer your question.

Juanjo
  • 79
  • 1
  • 8
0

In C and C++, arrays are second-class. They cannot be passed by value by themselves, only if somehow wrapped.

As a first step, the questions you have to decide are:

  1. Does your array have a fixed length?
  2. And do you have to pass it by value or can you pass it by reference?

If you have to pass it by value, is that a choice you want the caller to make, or the callee to impose? In the first case, pass it by reference.

If you pass the array by reference, nothing can beat using a gsl::span, unless you pass multiple sequences all having intrinsically the same length, in which case passing pointers and a single length-argument is more efficient and maybe comfortable.

If you pass an array of variable length by value, try to use a std::vector. That's also the go-to type to pass a by-ref argument as if by-value.

Otherwise (array of fixed length, by value), nothing beats std::array.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • not true, arrays can be first class, but only as long as they are defined inside a struct. this is analogous to std::array. – Dmytro Mar 17 '18 at 16:43
0

If p is a pointer to a function, which receives no parameters, you should call it by this syntax:

p();

So, if array is an array of pointers to functions, you should call one of them using the same syntax idea:

array[0]();

Here the parentheses are important; they say "call this function, and pass no parameters to it". If you have no parentheses

array[0];

this means "select this function from the array, but do nothing with it".

It's a useless expression, like if you have an integer x, then x * 5 means "multiply x by 5 and do nothing with the result" (useless), while x *= 5 means "multiply x by 5 and replace x with the result".

anatolyg
  • 26,506
  • 9
  • 60
  • 134