Solved

Array of function pointers

Posted on 1998-05-18
4
199 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 20 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now