By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,323 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.