6 Replies - 743 Views - Last Post: 21 March 2012 - 11:34 PM Rate Topic: -----

#1 Duaa Zahi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 ) ;


please help.
Is This A Good Question/Topic? 0
  • +

Replies To: dynamic table of pointers to functions

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • 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);
	ptr = add;
	cout << ptr(2,3) << endl;
	
	const int size = 3;
	func *array = new func[size];
	array[0] = add;
	cout << array[0](4,5) << endl;
	return 0;
}



Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#3 Duaa Zahi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :)
Was This Post Helpful? 0
  • +
  • -

#4 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • 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

Was This Post Helpful? 2
  • +
  • -

#5 Duaa Zahi  Icon User is offline

  • New D.I.C Head

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

Re: dynamic table of pointers to functions

Posted 21 March 2012 - 11:06 AM

View PostKarel-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
Was This Post Helpful? 0
  • +
  • -

#6 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • 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[0] = &add_two;
    p[1] = &subtract_two;
    cout<<"Add: 2+3 = "<<p[0](2,3)<<endl;
    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.
More Reading Here

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

Was This Post Helpful? 1
  • +
  • -

#7 Duaa Zahi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1