On 6 Mar, 17:27, "Alf P. Steinbach" <al...@[EMAIL PROTECTED]
> wrote:
> * Andrew Falanga:
> > =A0About two weeks ago (or so) I posted here that I was having
problems
> > with getting a member function pointer to point to a member function
> > without giving me grief.
>
> Yes, as a novice you should not use member functions /at all/.
>
At least not through pointers :)
>
> > I've gone to trying this on a trial program
> > that I will post below in its entirety. =A0I just can't get it right
and=
> > although I've tried the suggestions given to me from the other
> > posting, I'm still doing something wrong. =A0So, here's my test
program:=
>
> > #include <iostream>
>
> > class base {
> > =A0 =A0int somedata;
>
> > =A0 =A0public:
> > =A0 =A0base();
>
> > =A0 =A0int GetData() {
> > =A0 =A0 =A0 return somedata;
> > =A0 =A0}
>
> > =A0 =A0int (base::*ptrMemFunc)();
>
> > };
>
> > base::base() : somedata(5), ptrMemFunc(&base::GetData)
> > {}
>
> > // this one's only in here to make sure I remembered how to use
> > function pointers
> > int AnInt() {
> > =A0 =A0return 5;
> > }
>
> > int main( ) {
> > =A0 =A0base b;
> > =A0 =A0int (*pAnInt)() =3D AnInt;
>
> > =A0 =A0std::cout << pAnInt() << std::endl;
>
> > =A0 =A0b.ptrMemFunc();
>
> "b.ptrMemFunc" is an expression that refers to a non-static member
functio=
n pointer.
>
> "()" tries to call the referred non-static member function, without
specif=
ying
> an object.
>
> That's meaningless.
>
> > =A0 =A0return 0;
> > }
>
> [snip]
>
>
>
> > So, how do I get a class member, that is a function pointer, point to
> > another member function of that class, and the use that pointer to
> > reference these functions in other parts of the code that I'm trying
> > to?
>
> Don't.
>
> Use virtual member functions.
Yes, I think you should look at design and see if there is a way to do
what you want without using a pointer to member function. But I think
the answer you are looking for is:
(b.*b.ptrMemFunc)();
Hope this helps
DP


|