public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Member function pointers
@ 2005-05-24 14:30 Rune Torgersen
  2005-05-24 16:18 ` Lionel B
       [not found] ` <DCEAAC0833DD314AB0B58112AD99B93B85939A@ismail.innsys.innov sys.com>
  0 siblings, 2 replies; 4+ messages in thread
From: Rune Torgersen @ 2005-05-24 14:30 UTC (permalink / raw)
  To: gcc-help

I have a piece of code that worked perfectly under GCC 3.3.3, but fails
to even compile under GCC 3.4.3


class testfnptr
{
public:
    bool (testfnptr::*func)(void);

    testfnptr()	{func = 0; };

    bool dotest()
    {
        if (0 != (this->*func))
        {
            (this->*func)();
        }
	}
};

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.


Rune Torgersen
System Developer
Innovative Systems LLC
1000 Innovative Drive
Mitchell, SD 57301
Ph: 605-995-6120
www.innovsys.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Member function pointers
  2005-05-24 14:30 Member function pointers Rune Torgersen
@ 2005-05-24 16:18 ` Lionel B
       [not found] ` <DCEAAC0833DD314AB0B58112AD99B93B85939A@ismail.innsys.innov sys.com>
  1 sibling, 0 replies; 4+ messages in thread
From: Lionel B @ 2005-05-24 16:18 UTC (permalink / raw)
  To: gcc-help

"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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Member function pointers
       [not found] ` <DCEAAC0833DD314AB0B58112AD99B93B85939A@ismail.innsys.innov sys.com>
@ 2005-05-24 16:20   ` Eljay Love-Jensen
  2005-05-24 16:23     ` corey taylor
  0 siblings, 1 reply; 4+ messages in thread
From: Eljay Love-Jensen @ 2005-05-24 16:20 UTC (permalink / raw)
  To: Rune Torgersen, gcc-help

Hi Rune,

func is a data member of the testfnptr object.

Change this...
if (0 != (this->*func))
...to this...
if (0 != func)

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Member function pointers
  2005-05-24 16:20   ` Eljay Love-Jensen
@ 2005-05-24 16:23     ` corey taylor
  0 siblings, 0 replies; 4+ messages in thread
From: corey taylor @ 2005-05-24 16:23 UTC (permalink / raw)
  To: Eljay Love-Jensen; +Cc: Rune Torgersen, gcc-help

In section 5.3.1 paragraph 1 and paragraph 3 of the C++ spec, I
believe you'll find the reason for that error:

Paragraph 1:

The unary * operator performs indirection: the expression to which it
is applied shall be a pointer to an object type, or a pointer to a
function type and the result is an lvalue referring to the object or
function to which the expression points. If the type of the expression
is "pointer to T," the type of the result is "T." [Note: a pointer to
an incomplete type (other than cv void ) can be dereferenced. The
lvalue thus obtained can be used in limited ways (to initialize a
reference, for example); this lvalue must not be converted to an
rvalue, see 4.1. ]

Paragraph 3:

A pointer to member is only formed when an explicit & is used and its
operand is a qualified-id not enclosed in parentheses. [Note: that is,
the expression &(qualified-id), where the qualified-id is enclosed in
parentheses, does not form an expression of type "pointer to member."
Neither does qualified-id, because there is no implicit conversion
from a qualified-id for a nonstatic member function to the type
"pointer to member function" as there is from an lvalue of function
type to the type "pointer to function" (4.3). Nor is &unqualified-id a
pointer to member, even within the scope of the unqualified-id's
class. ]

corey

On 5/24/05, Eljay Love-Jensen <eljay@adobe.com> wrote:
> Hi Rune,
> 
> func is a data member of the testfnptr object.
> 
> Change this...
> if (0 != (this->*func))
> ...to this...
> if (0 != func)
> 
> HTH,
> --Eljay
> 
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-05-24 16:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-24 14:30 Member function pointers Rune Torgersen
2005-05-24 16:18 ` Lionel B
     [not found] ` <DCEAAC0833DD314AB0B58112AD99B93B85939A@ismail.innsys.innov sys.com>
2005-05-24 16:20   ` Eljay Love-Jensen
2005-05-24 16:23     ` corey taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).