473,395 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Initializing an array of pointers to member functions insideconstructor & invoking them

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 function pointer array
(Handlers) - compiler doesnt let me do that.
I got an error as below:

"invalid use of non-static member function bool test::func1(long int)"

I am looking for two things at this point

1) how to initialize the member array of pointers (I prefer to do that
in the constructor)

2) call them from a member function funcw by their index?

Here is my code:

using namespace std;
#include <iostream>
#define MAXFN 2

class test;

typedef bool (*pfn) (long);

class test {

private:

long var;
pfn Handlers[MAXFN];

public:

test();
bool func1(long);
bool func2(long);
bool funcw(long);
void reg();
};
test::test() {
}

bool test::func1(long _p1) {
var = _p1;
}

bool test::func2(long _p2) {

cout << var << endl;
cout << "New Value :" << var << endl;

}

bool test::funcw(long _i) {

}

void test::reg() {
}

main() {

test t1;
t1.func1(10);
t1.func2(20);
t1.funcw(1);

}

Thanks
/R
Jul 30 '08 #1
3 2784
Ramesh wrote:
I am trying to create an array of pointers to member functions
And your array is of pointers to functions. There is a significant
difference.
inside
my class. When I created a global array of type pfn & initialized with
member functions and copy it back to the member function pointer array
(Handlers) - compiler doesnt let me do that.
I got an error as below:

"invalid use of non-static member function bool test::func1(long int)"
Correct. A [pointer to a] non-static member cannot be converted to a
pointer to function. Those are incompatible.
>
I am looking for two things at this point

1) how to initialize the member array of pointers (I prefer to do that
in the constructor)
You cannot _initialise_ it. You can only assign to its elements.
>
2) call them from a member function funcw by their index?

Here is my code:

using namespace std;
#include <iostream>
#define MAXFN 2

class test;

typedef bool (*pfn) (long);
typedef bool (test::*pfn)(long);
>
class test {

private:

long var;
pfn Handlers[MAXFN];

public:

test();
bool func1(long);
bool func2(long);
bool funcw(long);
void reg();
};
test::test() {
Handlers[0] = &test::func1;
Handlers[1] = &test::func2;
}

bool test::func1(long _p1) {
var = _p1;
}

bool test::func2(long _p2) {

cout << var << endl;
cout << "New Value :" << var << endl;

}

bool test::funcw(long _i) {
Not sure about the meaning of '_i'. Using as the argument for all Handlers:

(this->*Handlers[0])(_i);
(this->*Handlers[1])(_i);

Using as the index:

(this->*Handlers[_i])(666);
}

void test::reg() {
}

main() {
int main() {
>
test t1;
t1.func1(10);
t1.func2(20);
t1.funcw(1);

}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 30 '08 #2
On Jul 30, 2:10*pm, Ramesh <rrame...@gmail.comwrote:
I am trying to create an array of pointers to member functions inside
my class. [snip]
Victor gave you the proper synatx, and further explanation can be
found in the FAQs:

http://www.parashift.com/c++-faq-lit...o-members.html

Cheers! --M
Jul 30 '08 #3
On Jul 30, 1:01*pm, mlimber <mlim...@gmail.comwrote:
On Jul 30, 2:10*pm, Ramesh <rrame...@gmail.comwrote:
I am trying to create an array of pointers to member functions inside
my class. [snip]

Victor gave you the proper synatx, and further explanation can be
found in the FAQs:

http://www.parashift.com/c++-faq-lit...o-members.html

Cheers! --M
Thanks Victor!

I read the FAQ though, feel I should invoke member functions through
another member function rather than a macro - just to make debugging
easier.

Regards
/R
Jul 30 '08 #4

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

Similar topics

2
by: Neno | last post by:
Hi, I have a linkage error that has something to do with the use of a static member array and I can't understand how to solve the problem. Can someone help me please? In detail: A class Month...
5
by: Steve | last post by:
Can anyone tell me if I can have an array of functions that take a variable number of parameters? If it is possible I'd like to know how to declare the array and the functions as its elements. I am...
5
by: amparikh | last post by:
I have some test code which demonstrates the problem. I know I could solve this by just returning a pointer, but I better use a reference. In real code, what I actually want to return is a...
3
by: Bilgehan.Balban | last post by:
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? ...
7
by: nk | last post by:
Hi, I'm a newbie on this language. I would be very happy if you help me about the following issue: The code below, reads some names(strings), stores them, and stores the addresses in the pointer...
22
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
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...

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.