473,657 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to use enum with array of function pointers ?

1 New Member
Hello all,

I want to initialize the pointers to functions, which i have declared in an array.
e.g

#include <math.h>
typedef double (*Fx)(double x); // pointer to function

int main(void)
{
Fx f[3]; //array of function pointers

f[0] = cos;
f[1] = sin;
f[2] = tan;
.
.
.
.
.
}
This approach is ok if the number of functions is small.

If I have ,say 200 functions and I want to use pointer to function, how can i initialize so many pointers?
I was thinking about using enum for storing name of functions, but cannot implement exactly as i doubt whether if i use enum instance instead of function name, will it work?

Plz do suggest me...
Aug 13 '07 #1
2 10972
weaknessforcats
9,208 Recognized Expert Moderator Expert
If you have 200 functions you don't need 200 function pointers unless every function has different arguiments ro return types.

The idea of the function pointer is to decide at run time what function to call based on runtime coinditions.

An enum is just a named integer value. Enums are used to avoid harde-coding integers in your program.

You could use an enum to help you decide what function to call.

Assume you have two functions:
Expand|Select|Wrap|Line Numbers
  1. int MyFunction(int, int);
  2. int YourFunction(int, int);
  3. //and a function pointer:
  4.  
  5. int (*fp)(int, int);
  6.  
  7.  
You could do this:

Expand|Select|Wrap|Line Numbers
  1. enum funcs {MYFUNCTION, YOURFUNCTION};
  2. .....
  3.  
  4. switch (funcs)
  5. {
  6.     case MYFUNCTION:
  7.             fp = MyFunction;
  8.             break;
  9.     case YOURFUNCTION:
  10.             fp = YourFunction;
  11.             break;
  12. ....
  13. }
  14.  
Aug 13 '07 #2
RRick
463 Recognized Expert Contributor
You can also use enums to designate the position of the function within the array.

Expand|Select|Wrap|Line Numbers
  1. enum  { Sin=0, Cos, ... };
  2. ........
  3. // Initialization
  4. fx[ Sin] = ::sin;
  5. fx[ Cos] = ::cos;
  6. ........
  7. double sinValue = fx[Sin]( .8999);  //  Assuming radians are used
  8.  
Now you control how the pointers are used.

One other point, only the functions that exactly match the Fx typedef will compile with C++. If you have a function that takes 2 parameters, then the compiler will complain.
Aug 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

58
10121
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
2
4278
by: Voronkov Konstantin | last post by:
Thank you for answer, but I still did not got *how* to make serialization of enum type. Can you provide more instructions or hint, please? My task is to serialize enum to something like byte array, and then load enum from that byte array to the same enum type on other platform. One way to do I see is:
10
10501
by: Russell Shaw | last post by:
Hi, gcc-3.4 complains about non-integers in: enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"}; even if i cast the strings to integers.
6
6168
by: Marc Scheuner [MVP ADSI] | last post by:
Folks, I was trying to achieve the following thing: I have a "structure" that basically is a two-dimensional array of numeric values. Both dimensions are handled by enumeration types in C#: public enum Level = { low, medium, high }; public enum Foo = { bar, subbar, superbar }; Now my idea was to create a two-dimensional array using those enum's
4
1478
by: UJ | last post by:
I know how to define enums, and they work great. But is there any way to define the same time of thing but give them actual values? For instance, I'd like to define something with the idea like this: Public Enum TMCContentStatus as string PrevApproved = "P" ToBeAdded = "A"
12
3341
by: Laurent Deniau | last post by:
If I understand well, an enumeration is only garantee to hold at most an int (6.7.2.2-2). So I would like to know: how to store a long in an enum? enum { p2_31 = 1L << 31 }; // boom how to define a synonym of a constant address?
7
7950
by: Kevin | last post by:
I'm creating a template to support state machines. In doing so, I need to pass an enumeration for the number of transitions and a non type parameter for the range of the enum (to allow me to allocate an array of pointers big enough to hold one entry for each transition). My template declaration looks roughly like this: template <class TRANSITION, int NUMTRANSITIONS> class StateMachine { ***SNIP*** void RegisterTransition(TRANSITION...
7
8277
by: Travis | last post by:
Just curious, which takes less memory? enum Months { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; static const char *Months =
2
2974
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your standard dynamic address book program. Adding, and listing work just fine. However, deleting, editing and viewing relies on member function retAddress. This function returns an array of pointers that are pointing to created objects. In action, all it...
0
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.