Function Pointer as a member variable 
Author Message
 Function Pointer as a member variable

I hope you guys can help.  I've got a class that I  would
like to use a function pointer in.
In my header file, I declare the function pointer as such:

bool (*updateDisplay)();

The functions that it will point to are also members of
this same class, as such:

bool updateScreen();
bool updateWindow();

Doing so this way gives me a compile error about failing
to cast upon assignment to the actuall function, so I
declared my function pointer like so:

bool (classname::*updateDisplay)();

That seemed to work, however, when I make a call to it,
I get a compile error stating that the call doesn't
evaluate to a function.

Has anyone done this?  I don't expect this is too
difficult, as I believe all member functions are in a
sense doing this anyway, or not.
Anyway, any help would be greatly appreciated.
Thanks.



Mon, 17 Jan 2005 04:46:52 GMT  
 Function Pointer as a member variable

Quote:

> I hope you guys can help.  I've got a class that I  would
> like to use a function pointer in.
> In my header file, I declare the function pointer as such:

> bool (*updateDisplay)();

So, this is a pointer to function.

Quote:

> The functions that it will point to are also members of
> this same class

Wait, wait...  Pointer-to-function is not the same thing as
pointer-to-non-static-member.  They are incompatible.

Quote:
>, as such:

> bool updateScreen();
> bool updateWindow();

> Doing so this way gives me a compile error about failing
> to cast upon assignment to the actuall function, so I
> declared my function pointer like so:

> bool (classname::*updateDisplay)();

So, now it's a pointer-to-member.

Quote:

> That seemed to work, however, when I make a call to it,
> I get a compile error stating that the call doesn't
> evaluate to a function.

And _how_, if I may ask, do you call it?

Quote:

> Has anyone done this?

You better believe it!

Quote:
>  I don't expect this is too
> difficult, as I believe all member functions are in a
> sense doing this anyway, or not.

You don't need to believe in anything.  Just get a good book
and read the appropriate section.  It's not religion, it's
technology.

--------------------------------------------------------
struct A
{
    int foo() { return 42; }
    int bar() { return 73; }

Quote:
};

typedef int (A::*ptrToMemOfA)();

int main()
{
    ptrToMemOfA ptr;
    ptr = &A::foo;
    A a; // need an object to call a non-static member
    int result_one = (a.*ptr)();
    ptr = &A::bar;
    int result_two = (a.*ptr)();

    return 0;

Quote:
}

--------------------------------------------------------

Victor
--
Please remove capital A's from my address when replying by mail



Mon, 17 Jan 2005 05:16:12 GMT  
 Function Pointer as a member variable
Try this program:

#include <stdio.h>
class classname
{
public:
bool (classname::*updateDisplay)();
bool updateScreen() { printf ("updateScreen\n"); return
true; }
bool updateWindow() { printf ("updateWindow\n"); return
true; }
  void test ()
  {
      updateDisplay = updateScreen;
      (this->*updateDisplay)();
  }

Quote:
};

main ()
{
    classname x;
    x.test(); // must print 'updateScreen'
    x.updateDisplay = classname::updateWindow;
    (x.*x.updateDisplay)(); // must print 'updateWindow'

    return 0;

Quote:
}

I dunno why I must use this awkward notation    
(x.*x.updateDisplay)();
maybe there will be a simpler way....

Edson E. W.

Quote:
>-----Original Message-----
>I hope you guys can help.  I've got a class that I  would
>like to use a function pointer in.
>In my header file, I declare the function pointer as such:

>bool (*updateDisplay)();

>The functions that it will point to are also members of
>this same class, as such:

>bool updateScreen();
>bool updateWindow();

>Doing so this way gives me a compile error about failing
>to cast upon assignment to the actuall function, so I
>declared my function pointer like so:

>bool (classname::*updateDisplay)();

>That seemed to work, however, when I make a call to it,
>I get a compile error stating that the call doesn't
>evaluate to a function.

>Has anyone done this?  I don't expect this is too
>difficult, as I believe all member functions are in a
>sense doing this anyway, or not.
>Anyway, any help would be greatly appreciated.
>Thanks.

>.



Mon, 17 Jan 2005 05:45:54 GMT  
 Function Pointer as a member variable
Try this program:

#include <stdio.h>
class classname
{
public:
bool (classname::*updateDisplay)();
bool updateScreen() { printf ("updateScreen\n"); return
true; }
bool updateWindow() { printf ("updateWindow\n"); return
true; }
  void test ()
  {
      updateDisplay = updateScreen;
      (this->*updateDisplay)();
  }

Quote:
};

main ()
{
    classname x;
    x.test(); // must print 'updateScreen'
    x.updateDisplay = classname::updateWindow;
    (x.*x.updateDisplay)(); // must print 'updateWindow'

    return 0;

Quote:
}

I dunno why I must use this awkward notation    
(x.*x.updateDisplay)();
maybe there will be a simpler way....

Edson E. W.

Quote:
>-----Original Message-----
>I hope you guys can help.  I've got a class that I  would
>like to use a function pointer in.
>In my header file, I declare the function pointer as such:

>bool (*updateDisplay)();

>The functions that it will point to are also members of
>this same class, as such:

>bool updateScreen();
>bool updateWindow();

>Doing so this way gives me a compile error about failing
>to cast upon assignment to the actuall function, so I
>declared my function pointer like so:

>bool (classname::*updateDisplay)();

>That seemed to work, however, when I make a call to it,
>I get a compile error stating that the call doesn't
>evaluate to a function.

>Has anyone done this?  I don't expect this is too
>difficult, as I believe all member functions are in a
>sense doing this anyway, or not.
>Anyway, any help would be greatly appreciated.
>Thanks.

>.



Mon, 17 Jan 2005 05:46:02 GMT  
 Function Pointer as a member variable
You're not paying attention "Mr. It's not religion, it's
technology"
Hey, if you want to be a{*filter*}, go someplace else.
Back to the matter at hand.
You missed one very important point.  The function
pointer should also be a member of the class.
All of the "Fucntion pointer stuff" should be transparant
to the user of the class.

Quote:
>-----Original Message-----

>> I hope you guys can help.  I've got a class that I  
would
>> like to use a function pointer in.
>> In my header file, I declare the function pointer as
such:

>> bool (*updateDisplay)();

>So, this is a pointer to function.

>> The functions that it will point to are also members of
>> this same class

>Wait, wait...  Pointer-to-function is not the same thing
as
>pointer-to-non-static-member.  They are incompatible.

>>, as such:

>> bool updateScreen();
>> bool updateWindow();

>> Doing so this way gives me a compile error about
failing
>> to cast upon assignment to the actuall function, so I
>> declared my function pointer like so:

>> bool (classname::*updateDisplay)();

>So, now it's a pointer-to-member.

>> That seemed to work, however, when I make a call to it,
>> I get a compile error stating that the call doesn't
>> evaluate to a function.

>And _how_, if I may ask, do you call it?

>> Has anyone done this?

>You better believe it!

>>  I don't expect this is too
>> difficult, as I believe all member functions are in a
>> sense doing this anyway, or not.

>You don't need to believe in anything.  Just get a good
book
>and read the appropriate section.  It's not religion,
it's
>technology.

>--------------------------------------------------------
>struct A
>{
>    int foo() { return 42; }
>    int bar() { return 73; }
>};

>typedef int (A::*ptrToMemOfA)();

>int main()
>{
>    ptrToMemOfA ptr;
>    ptr = &A::foo;
>    A a; // need an object to call a non-static member
>    int result_one = (a.*ptr)();
>    ptr = &A::bar;
>    int result_two = (a.*ptr)();

>    return 0;
>}
>--------------------------------------------------------

>Victor
>--
>Please remove capital A's from my address when replying
by mail

>.



Mon, 17 Jan 2005 05:47:16 GMT  
 Function Pointer as a member variable

Quote:

> You're not paying attention "Mr. It's not religion, it's
> technology"
> Hey, if you want to be a{*filter*}, go someplace else.

Don't tell me where to go or what to do.  If you have
problems (whether with the language or with your own
head, don't bring it out on me).

Quote:
> Back to the matter at hand.
> You missed one very important point.  The function
> pointer should also be a member of the class.

If you posted in C++ instead of English, it would be much
harder to miss.

Quote:
> All of the "Fucntion pointer stuff" should be transparant
> to the user of the class.

Go tell that to the people who design the language.

In the case of a pointer being a member of the class, there
is special syntax.  You still HAVE to tell the compiler which
_object_ the pointer-to-member applies to.

struct A
{
    typedef int (A::*ptrToMemOfA)();

    int foo() { return 42; }
    int bar() { return 73; }

    ptrToMemOfA ptr;

    void setMemPtr(ptrToMemOfA p) { ptr = p; }

    int useMemPtr()
    {
        return (this->*ptr)();  // special syntax
    }

Quote:
};

int main()
{
    A a; // need an object to call a non-static member
    a.setMemPtr(&A::foo);
    int result_one = a.useMemPtr();
    a.setMemPtr(&A::bar);
    int result_two = a.useMemPtr();

    return 0;

Quote:
}

Now, if _that_ is not what you want, post some code yourself,
damn it.  I am not a mind reader.
Quote:

> >-----Original Message-----

> >> I hope you guys can help.  I've got a class that I
> would
> >> like to use a function pointer in.
> >> In my header file, I declare the function pointer as
> such:

> >> bool (*updateDisplay)();

> >So, this is a pointer to function.

> >> The functions that it will point to are also members of
> >> this same class

> >Wait, wait...  Pointer-to-function is not the same thing
> as
> >pointer-to-non-static-member.  They are incompatible.

> >>, as such:

> >> bool updateScreen();
> >> bool updateWindow();

> >> Doing so this way gives me a compile error about
> failing
> >> to cast upon assignment to the actuall function, so I
> >> declared my function pointer like so:

> >> bool (classname::*updateDisplay)();

> >So, now it's a pointer-to-member.

> >> That seemed to work, however, when I make a call to it,
> >> I get a compile error stating that the call doesn't
> >> evaluate to a function.

> >And _how_, if I may ask, do you call it?

> >> Has anyone done this?

> >You better believe it!

> >>  I don't expect this is too
> >> difficult, as I believe all member functions are in a
> >> sense doing this anyway, or not.

> >You don't need to believe in anything.  Just get a good
> book
> >and read the appropriate section.  It's not religion,
> it's
> >technology.

> >--------------------------------------------------------
> >struct A
> >{
> >    int foo() { return 42; }
> >    int bar() { return 73; }
> >};

> >typedef int (A::*ptrToMemOfA)();

> >int main()
> >{
> >    ptrToMemOfA ptr;
> >    ptr = &A::foo;
> >    A a; // need an object to call a non-static member
> >    int result_one = (a.*ptr)();
> >    ptr = &A::bar;
> >    int result_two = (a.*ptr)();

> >    return 0;
> >}
> >--------------------------------------------------------

> >Victor
> >--
> >Please remove capital A's from my address when replying
> by mail

> >.



Mon, 17 Jan 2005 06:03:01 GMT  
 Function Pointer as a member variable


Quote:
> You're not paying attention "Mr. It's not religion, it's
> technology"
> Hey, if you want to be a{*filter*}, go someplace else.

You should go somewhere else.  After proving
yourself to be an {*filter*} here, few people
are going to be willing to attempt to help
you as Victor has so kindly done.

Quote:
> Back to the matter at hand.
> You missed one very important point.

As far as I could see, Victor's original
reply covered the relevant points.  You
need to read it more carefully after a
sincere attempt to improve your attitude.

PLONK.

--
-Larry Brasfield
(address munged, s/sn/h/ to reply)



Mon, 17 Jan 2005 06:31:01 GMT  
 Function Pointer as a member variable

says...

Quote:
> class classname
> {
> public:
> bool (classname::*updateDisplay)();
> bool updateScreen() { printf ("updateScreen\n"); return
> true; }
> bool updateWindow() { printf ("updateWindow\n"); return
> true; }
>   void test ()
>   {
>       updateDisplay = updateScreen;
>       (this->*updateDisplay)();
>   }
> };

> main ()
> {
>     classname x;
>     x.test(); // must print 'updateScreen'
>     x.updateDisplay = classname::updateWindow;
>     (x.*x.updateDisplay)(); // must print 'updateWindow'

>     return 0;
> }

> I dunno why I must use this awkward notation    
> (x.*x.updateDisplay)();
> maybe there will be a simpler way....

> Edson E. W.

Well, I wouldn't want to change the language, now that the standards are
in place and thousands of people are using it this way.  Even if we
consider changing the language, what better syntax could you suggest?  
(Remember that it must not be ambiguous, or the compiler would be unable
to resolve it.)


Mon, 17 Jan 2005 11:40:23 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. static function access member variable and member function

2. pointer to member function/variable info?

3. member function and member variable

4. member function and member variable

5. member function and member variable

6. Pointers to member functions as function arguments

7. (member) function returning pointer to functions like itself?

8. Getting pointer to non-static member function from C callback function

9. Function Pointer for Member Function

10. Function pointer for Member Function

11. function pointer to a member function

12. Function pointer / function member

 

 
Powered by phpBB® Forum Software