?
Solved

Array of function pointers

Posted on 1998-05-18
4
Medium Priority
?
213 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

764 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