?
Solved

Array of function pointers

Posted on 1998-05-18
4
Medium Priority
?
215 Views
Last Modified: 2013-12-26
Is it possible to create an array of function pointers and if so how ?
0
Comment
Question by:ShriramS
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
msmits earned 80 total points
ID: 1299676
It should not be necessary in C++ and object-oriented programming to use arrats of function pointer, but technically it is possible.

void (*pfa[10])(char*);

Which is an array of 10 pointers to functions that return void and take a char* as parameter.

void xyz(char *x)
{
}

pfa[0] = xyz;

0
 
LVL 1

Author Comment

by:ShriramS
ID: 1299677
What I need to do is have a array of pointers in a class without knowing how many items I need in the array. How do I deal with that ? Can I declare a pointer in the declaration and then define it as an array in the constructor for example ?
0
 
LVL 3

Expert Comment

by:msmits
ID: 1299678
That depends on when you know how may pointer you need. If you know it at constructor time, you can create a pointer to an array of pointers and allocate the memory at constructor time for just the number of elements you need.

void (**pfa)(char*);

void xyz(char *x)
{}

X:x(int n) {
  pfa = new (void (*[n])(char *));
  pfa[0] = xyz;
}

pfa[0]("hello");

If you want real dynamic array, you need to use the vector class from the Standard Library.

0
 
LVL 1

Author Comment

by:ShriramS
ID: 1299679
Thank You
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question