Mail Archives: djgpp/1999/09/01/01:08:38
On Tue, 31 Aug 1999 13:36:51 +0200, flupke <schawat AT club-internet DOT fr>
wrote:
> I don't know if i'm doing a "c++ misunderstanding" or if it's a djgpp
> problem, but here is my problem anyway.
> When i execute this piece of code:
> ***********************
> #include <iostream.h>
>
> class glou
> {
> public:
> int c;
>
> glou()
> {
> c = 0;
> a = &b;
> }
>
> void (*a)();
>
> void b()
> {
> c++;
> }
> };
>
> int main()
> {
> glou blou;
>
> cout << blou.c << endl;
>
> blou.a();
> cout << blou.c << endl;
>
> blou.b();
> cout << blou.c << endl;
>
> return 0;
> }
The correct syntax handling this situation is
#include <iostream.h>
class glou
{
public:
int c;
glou()
{
c = 0;
pmf = &glou::b; // !!!
}
void (glou::*pmf)(); // !!!
void b() { c++; }
};
int main()
{
glou blou,blou1;
void (glou::*pmf1)() = &glou::b;
cout << blou.c << endl;
(blou.*blou.pmf)(); // calling though blou's pmf
cout << blou.c << endl;
blou.b();
cout << blou.c << endl;
(blou.*pmf1)(); // calling though local pfm1
cout << blou.c << endl;
(blou.*blou1.pmf)(); // calling through blou1's pmf
cout << blou.c << endl;
return 0;
}
You absolutely need a pointer of type
void (glu::*)()
in order to call a non-static member function of b's type by pointer.
You have to call the function through the pointer using the .*
operator (or the ->* operator if the calling object is addressed
through a glue*)
Note that the .* or ->* operator is _not_ a kind-of . or -> operator,
i.e. it does _not_ select fields of the object. Note that the pointer
may be defined outside of the class (pmf1) or in another object
(blou1.pmf).
.* and ->* have a lower precedence than the () operator. Therefore
you have to put brackets around the function expression
(blou.*blou.pfm)()
before applying the function call operator ().
Regards
Horst
- Raw text -