C++ Pointer to Dynamic Array of functions?

user5478212

I've created an array of pointer to functions, I want to know Is it possible to create array of pointer dynamically, as you see in below I want to change array length dynamically which currently is 2.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

void func1(int);
void func2(int);

int main()
{
    void (*func[2])(int) = { &func1, &func2 };

    func[0](10);
    func[1](20);

    cin.ignore();
    return 0;
}

void func1(int n)
{
    cout << "In func1()\n\tThe value is: " << n << endl;
}

void func2(int n)
{
    cout << "In func2()\n\tThe value is: " << n << endl;
}
Joey Yandle

Make a typedef for the function type:

typedef void (*FunctionType)(int);

Then make a normal dynamic array:

FunctionType* func = new FunctionType[2];

Then you can assign:

func[0] = &func1;

And call:

func[0](1);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ Pointer to Dynamic Array of functions?

From Dev

Pointer to array of functions in C

From Dev

C - Pointer to dynamic array of structs

From Dev

C++ method to return a dynamic pointer array

From Dev

Pointer within struct to dynamic array of structures C

From Dev

Dynamic array with void pointer

From Dev

"Dynamic (pointer) arrays" across functions

From Dev

C - pointer to pointer array

From Dev

Pointer being freed was not allocated, dynamic array C++

From Dev

C++ Passing Dynamic Pointer to a 2D Array

From Dev

C dynamic allocation of a 2D array in a function & return pointer

From Dev

Using a "double-pointer" dynamic array in C++

From Dev

C passing dynamic array through pointer (Segmentation Fault)

From Dev

C - Pointer to dynamic array in struct "Segmentation fault (core dumped)"

From Dev

How to initialize a pointer to a dynamic 2D Array in C++

From Dev

C pointer and dynamic alloc

From Dev

working of pointer to functions in c

From Dev

C pointer to multiple functions

From Dev

Testing Pointer Functions in C

From Dev

How to pointer reference dynamic sized pointer to array?

From Dev

How to pointer reference dynamic sized pointer to array?

From Dev

pointer to dynamic array of pointers to dynamic array of pointer that points to strings

From Dev

Aligned dynamic array and smart pointer

From Dev

C pointer referencing another pointer in functions

From Dev

Dynamic array C++, trouble with new Obj[size] creating only 1 Object pointer, not array

From Dev

Dynamic array C++, trouble with new Obj[size] creating only 1 Object pointer, not array

From Dev

Dynamic Arary in C++ with pointer

From Dev

Functions with pointer arguments in C++

From Dev

Porting pointer functions to c#

Related Related

  1. 1

    C++ Pointer to Dynamic Array of functions?

  2. 2

    Pointer to array of functions in C

  3. 3

    C - Pointer to dynamic array of structs

  4. 4

    C++ method to return a dynamic pointer array

  5. 5

    Pointer within struct to dynamic array of structures C

  6. 6

    Dynamic array with void pointer

  7. 7

    "Dynamic (pointer) arrays" across functions

  8. 8

    C - pointer to pointer array

  9. 9

    Pointer being freed was not allocated, dynamic array C++

  10. 10

    C++ Passing Dynamic Pointer to a 2D Array

  11. 11

    C dynamic allocation of a 2D array in a function & return pointer

  12. 12

    Using a "double-pointer" dynamic array in C++

  13. 13

    C passing dynamic array through pointer (Segmentation Fault)

  14. 14

    C - Pointer to dynamic array in struct "Segmentation fault (core dumped)"

  15. 15

    How to initialize a pointer to a dynamic 2D Array in C++

  16. 16

    C pointer and dynamic alloc

  17. 17

    working of pointer to functions in c

  18. 18

    C pointer to multiple functions

  19. 19

    Testing Pointer Functions in C

  20. 20

    How to pointer reference dynamic sized pointer to array?

  21. 21

    How to pointer reference dynamic sized pointer to array?

  22. 22

    pointer to dynamic array of pointers to dynamic array of pointer that points to strings

  23. 23

    Aligned dynamic array and smart pointer

  24. 24

    C pointer referencing another pointer in functions

  25. 25

    Dynamic array C++, trouble with new Obj[size] creating only 1 Object pointer, not array

  26. 26

    Dynamic array C++, trouble with new Obj[size] creating only 1 Object pointer, not array

  27. 27

    Dynamic Arary in C++ with pointer

  28. 28

    Functions with pointer arguments in C++

  29. 29

    Porting pointer functions to c#

HotTag

Archive