Topic: HELP with function pointers to member variables
Author: pvnadham@uncc.edu (Prasanna V Nadhamuni)
Date: 1995/04/03 Raw View
In article <DONDZES.95Mar27152705@rapnet.sanders.lockheed.com>,
dondzes@rapnet.sanders.lockheed.com says...
>
>
>can someone please explain to me why this program will not compile ? I
am
>using g++ 2.4.3. I would _very_ much appreciate an email response.
>
In order to take the address of a member function of a c++ class, that
member function must static. This was setup by Bjorne Stroustrup.
or explicitly make it a pointer to member function of THAT
class.....
class A {.....};
int (A::*mfp)();
/* noe 'mfp' can be made to point to a member function of class A, the func
returns an integer and takes no args
*/
Author: BBogard@comptons.com (Brandy bogard)
Date: 1995/03/31 Raw View
In article <DONDZES.95Mar27152705@rapnet.sanders.lockheed.com>,
dondzes@rapnet.sanders.lockheed.com says...
>
>
>can someone please explain to me why this program will not compile ? I
am
>using g++ 2.4.3. I would _very_ much appreciate an email response.
>
In order to take the address of a member function of a c++ class, that
member function must static. This was setup by Bjorne Stroustrup.
Author: imp@village.org (Warner Losh)
Date: 1995/03/31 Raw View
In article <3lfi9i$ob2@newmedia.comptons.com>,
Brandy bogard <BBogard@comptons.com> wrote:
>In order to take the address of a member function of a c++ class, that
>member function must static. This was setup by Bjorne Stroustrup.
False. You can take the address of a member function, provided you
use it in a pointer to member context, and about half a dozen other
rules are followed, especially if you cast it.
Warner
Author: hhernan2@achp31.eld.ford.com (Humberto Hernandez)
Date: Tue, 28 Mar 1995 13:58:17 Raw View
Humberto Hernandez
University of Michigan
Author: hhernan2@achp31.eld.ford.com (Humberto Hernandez)
Date: Tue, 28 Mar 1995 15:11:07 Raw View
>class blob {
>private:
> char* (*fp)(); // my function pointer
To declare a pointer to a member function of blob do:
char *(blob::*fp)();
> char* f1() {return "1";};
> char* f2() {return "2";};
>public:
> void setup(int x){
> if (x == 1)
> fp = &(this->f1); // compiler error
> else
> fp = &(blob::f2); // compiler error
> }
>};
I am for the breif answer but I do not know what happened with my
previous and more elaborate response. I hope this helps. Good luck.
Humberto Hernandez
University of Michigan
Author: kelley@debbie.fbsw.tt.com (Kevin Kelley)
Date: 1995/03/29 Raw View
dondzes@rapnet.sanders.lockheed.com (David A. Ondzes x5-2707) writes:
>can someone please explain to me why this program will not compile ? I am
>using g++ 2.4.3. I would _very_ much appreciate an email response.
>David
Look up "pointer to member" or "member pointer" or some such in your
handy C++ book. A member function is a whole different kind of beast
from a "normal" function.
>------------------------------------------------------------------------
>#include <stdio.h>
>#include <stdlib.h>
>#include <stream.h>
>class blob {
>private:
> char* (*fp)(); // my function pointer
change above line to this:
char* (blob::*fp)();
> char* f1() {return "1";};
> char* f2() {return "2";};
>public:
> void setup(int x){
> if (x == 1)
> fp = &(this->f1); // still wrong
> else
> fp = &(blob::f2); // OK now.
> }
>};
>int main()
>{
> blob b;
> b.setup(1);
> b.setup(2);
>}
Kevin Kelley
** + '. -<>- o
****** O O ,' _ o
****** . '(_). +
** O ', kelleyk@ccmail.tt.com
Author: arnoud@ijssel.xs4all.nl (Arnoud Martens)
Date: 1995/03/28 Raw View
In article <DONDZES.95Mar27152705@rapnet.sanders.lockheed.com>,
David A. Ondzes x5-2707 <dondzes@rapnet.sanders.lockheed.com> wrote:
>#include <stdio.h>
>#include <stdlib.h>
>#include <stream.h>
>
>class blob {
>
>private:
> char* (*fp)(); // my function pointer
Make this char* (blob::*fp)(); and it compiles fine.
The comp.lang.c++ faq discusses the difference between a pointer to a
C function and to a C++ member function.
--
Name: Arnoud Martens, Utrecht, the Netherlands, tel: +31-30-732679
E-mail: arnoudm@ijssel.xs4all.nl WWW: http://www.xs4all.nl/~arnoudm
Author: dondzes@rapnet.sanders.lockheed.com (David A. Ondzes x5-2707)
Date: 27 Mar 1995 20:27:04 GMT Raw View
can someone please explain to me why this program will not compile ? I am
using g++ 2.4.3. I would _very_ much appreciate an email response.
David
------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stream.h>
class blob {
private:
char* (*fp)(); // my function pointer
char* f1() {return "1";};
char* f2() {return "2";};
public:
void setup(int x){
if (x == 1)
fp = &(this->f1); // compiler error
else
fp = &(blob::f2); // compiler error
}
};
int main()
{
blob b;
b.setup(1);
b.setup(2);
}