Solved

Array of function pointers

Posted on 1998-05-18
4
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

749 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