Re: Member function pointers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"Rune Torgersen" wrote in message:
> I have a piece of code that worked perfectly under GCC 3.3.3, but fails
> to even compile under GCC 3.4.3
>
>

#include <iostream>

> class testfnptr
> {
> public:
>     bool (testfnptr::*func)(void);
>
>     testfnptr() {func = 0; };
>
>     bool dotest()
>     {

           std::cout << "func = " << func << '\n';

>         if (0 != (this->*func))

You say you want to check if the member function pointer 'func' is NULL... so this test should be simply:

        if (0 != func)

i.e. you don't want to de-reference the pointer, since you are not calling any function (it wouldn't make sense - there
is no function to call ;-))

>         {
>             (this->*func)();
>         }
>     }

BTW, this function should return a value.

> };
>
> int main()
> {
> testfnptr testing;
> testing.dotest();
>
> return 0;
> }
>
> What I'm trying to do is to check if a member-function pointer is NULL
> before I call the function. (In my real application I have a function
> table that I go through, and some entries are set to NULL)
>
> GCC 3.4 3 says:
> testfnptr.cpp: In member function `bool testfnptr::dotest()':
> testfnptr.cpp:11: error: invalid use of non-static member function
>
> I have tried to check func directly, but that variable always have a
> value != 0.

Not on my system (GCC 3.4.3 also)... output is:

func = 0

I'm not entirely convinced the entire exercise is valid, however (I'm not sure what the C++ standard has to say about
permissible values for pointers to member functions). Perhaps safer would be to set a (member) flag to indicate whether
your member function pointer has been set to something sensible.

HTH,

-- 
Lionel B



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux