# dynamic table of pointers to functions

Page 1 of 1

## 6 Replies - 1168 Views - Last Post: 21 March 2012 - 11:34 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=271620&amp;s=d94ae48ccdabec97c440b48d3a95b198&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Duaa Zahi

Reputation: 0
• Posts: 30
• Joined: 19-March 12

# dynamic table of pointers to functions

Posted 19 March 2012 - 01:52 PM

Hi
how do we create dynamic table of pointers to functions ?
i tried this in C++ , but it doesn't work , syntax error!

```int (**ptr)(int , int )=new int [size] (int, int ) ;

```

Is This A Good Question/Topic? 0

## Replies To: dynamic table of pointers to functions

### #2 baavgai

• Dreaming Coder

Reputation: 6606
• Posts: 13,947
• Joined: 16-October 07

## Re: dynamic table of pointers to functions

Posted 19 March 2012 - 03:20 PM

I don't actually know the answer to this one...

That said, I don't hate myself enough to do this. It's more of a "how bad can the syntax get and still work" kind of problem.

I'd use a typedef and go from there. e.g.
```#include <iostream>

using namespace std;

int add(int x, int y) { return x+y; }

typedef int (*func)(int, int);

int main() {
int (*ptr)(int, int);
cout << ptr(2,3) << endl;

const int size = 3;
func *array = new func[size];
cout << array[0](4,5) << endl;
return 0;
}

```

Hope this helps.

### #3 Duaa Zahi

Reputation: 0
• Posts: 30
• Joined: 19-March 12

## Re: dynamic table of pointers to functions

Posted 20 March 2012 - 12:42 AM

Quote

That said, I don't hate myself enough to do this. It's more of a "how bad can the syntax get and still work" kind of problem.

hehehe
thanks that helped me

### #4 Karel-Lodewijk

Reputation: 454
• Posts: 864
• Joined: 17-March 11

## Re: dynamic table of pointers to functions

Posted 20 March 2012 - 03:12 AM

Well int (**ptr)(int, int) = new (int (*[size]) (int, int));, I had to try a few before I got that:). But if you care about readability at all, I'd go for:

```    typedef int (*Function_ptr_type) (int, int);
Function_ptr_type* ptr = new Function_ptr_type[size];

```

c++11:
```    auto ptr = new std::function<int(int,int)>[size];

```

The last two are not so bad actually imo.

This post has been edited by Karel-Lodewijk: 20 March 2012 - 03:29 AM

### #5 Duaa Zahi

Reputation: 0
• Posts: 30
• Joined: 19-March 12

## Re: dynamic table of pointers to functions

Posted 21 March 2012 - 11:06 AM

Karel-Lodewijk, on 20 March 2012 - 03:12 AM, said:

c++11:
```    auto ptr = new std::function<int(int,int)>[size];

```

Hi ,
Thank you for helping ...
Can you please explain to me the above line of code . i don't know this syntax.
Is "auto" a data type ?

Thank you
Duaa

### #6 Anarion

• The Persian Coder

Reputation: 385
• Posts: 1,661
• Joined: 16-May 09

## Re: dynamic table of pointers to functions

Posted 21 March 2012 - 02:39 PM

auto is a new feature of C++11 standard. Using this keyword, you don't need to tell the type of the variable explicitly. Read more information here.

And the std::function<> part is a polymorphic wrapper, also from the new standard. A wrapper can refer to functions of the same signature(same arguments and return type, or convertible). Example:
```#include <iostream>
#include <functional> //for the polymorphic wrapper
using namespace std;

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

int main ()
{
//w is a wrapper, and from here it refers to add_two function
//note that the return type and the arguments of the wrapper and function it refers to must be the same, or of the types that are convertible to it
function<int (int, int)> w = &add_two;
cout<<w(2, 3)<<endl;
return 0;
}
```

I am new to C++11 myself, after reading Karel-Lodewijk's post above I got curious to see these wrappers in work and wrote this for practice:
```#include <iostream>
#include <functional>
using namespace std;

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

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

int main ()
{
auto p = new function<int (int, int)>[2]; //allocate memory for two wrappers
//function<int (int, int)>* p = new function<int (int, int)>[2]; <- Without auto, you had to write this
//now, these pointers must refer to functions we have
p[1] = &subtract_two;
cout<<"Subtract: 3-2 = "<<p[1](3,2)<<endl;
delete[] p;
return 0;
}
```

That was interesting for me
A wrapper is not a pointer to function; but it's similar to one in syntax and semantics.

Using these new features depends on your compiler; recent versions of compilers have most of C++11 supported (I, myself, have tested GCC only).

This post has been edited by Anarion: 21 March 2012 - 04:44 PM

### #7 Duaa Zahi

Reputation: 0
• Posts: 30
• Joined: 19-March 12

## Re: dynamic table of pointers to functions

Posted 21 March 2012 - 11:34 PM

Thank you so much Anarion
i need some time to understand this but i think i got the idea
i will check the polymorphic wrapper concept .. it seems very interesting

Thanks for the explanation.
Duaa