473,471 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

array of function pointers for class functions

Hi,

How do I declare an array of function pointers and assign each element
of the array with public member functions of a class?

Is it possible that the array is not a member of the class?
Thanks,
Bahadir

Mar 12 '06 #1
3 6681
Bilgehan.Balban wrote:
How do I declare an array of function pointers and assign each element
of the array with public member functions of a class?
There's lots of ways. Report back with your actual problem to then learn the
best one.

Google for and check the FAQ for "member pointers".
Is it possible that the array is not a member of the class?


Why not? The pointers are just data; you can put them anywhere if they point
to _public_ member functions.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 12 '06 #2
<Bi*************@gmail.com> wrote:
How do I declare an array of function pointers and assign each element
of the array with public member functions of a class?

Is it possible that the array is not a member of the class?


This works.

#include <iostream>
using namespace std;

class C
{
public:
int f(int arg) {return 2*arg;}
int g(int arg) {return 3*arg;}
};
//...................
typedef int(C::*PF)(int);
//===================
int main()
{
C c;
C d;
PF pf[] = {&C::f, &C::g};
int n = (c.*pf[0])(3);
cout << n << ' ';
n = (d.*pf[1])(4);
cout << n << endl;
}
Mar 12 '06 #3

Phlip wrote:
Bilgehan.Balban wrote:
How do I declare an array of function pointers and assign each element
of the array with public member functions of a class?


There's lots of ways. Report back with your actual problem to then learn the
best one.

Google for and check the FAQ for "member pointers".
Is it possible that the array is not a member of the class?


Why not? The pointers are just data; you can put them anywhere if they point
to _public_ member functions.


Actually there is no requirement that the member pointers stored in an
external array point only to public member fuctions. It would be
possible for example to pass an array of member function pointers to an
instance of a class and have it fill the array with pointers to private
methods.

There is a requirement that all the member pointers in the array point
to methods with the same "signature" (number and type of parameters,
return type, const-volatile qualifications) - and that signature is
specified in the array's declaration.

Greg

Mar 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Evangelista Sami | last post by:
hello i haven't touch any C code for long time and i dont remember how to declare an array of pointers on functions. i have tried this : ----------------------- 35 void firing_1 36 (firing f,...
8
by: Sune | last post by:
Hi, I'm stuck at the moment with the following: I'm writing test functions for a module written in C. Since I'm lazy I want to type as little as possible. I came up with this idea: //...
13
by: munni | last post by:
hi i want to write a program on function pointers where i have not written any programs using function pointers till now. i want to take an array of 26 functions which r to be pointed by this...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
3
by: googlinggoogler | last post by:
Hi This should all be pretty standard C stuff, but I'm going to use terms like mouse callback to communicate what Im tyring to do. Basically I have my program whirling around in an infinite...
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
2
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...
2
by: Rahul | last post by:
Hi, I planning to replace the switch-case with an array of function pointers, the index for the array would be the case integers. Each case block would be implemented as a seperate function. It...
3
by: Ramesh | last post by:
Hi, I am trying to create an array of pointers to member functions inside my class. When I created a global array of type pfn & initialized with member functions and copy it back to the member...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.