pointer to member function

Discussion in 'C++' started by Imre Palik, Nov 9, 2005.

  1. Imre Palik

    Imre Palik Guest

    Hi'yal,

    consider this code:

    class A
    {
    class B
    {
    void (A::*mptr)();
    public:
    B();
    };

    void do_something();
    };

    void
    A::do_something() {}

    A::B::B() : mptr(do_something) {} // error here

    trying to compile it with gcc gives the following error:

    m.cpp:17: error: argument of type `void (A::)()' does not match `void (A::*)()'

    I fail to see the difference between these two types. Anybody cares to
    explain? Changing the problematic line to

    A::B::B() : mptr(&do_something) {} // error here

    I get the following error message:

    m.cpp:17: error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&A::do_something'

    I cannot find the relevant part of the standard. Could somebody point me
    to it?

    Thx

    ImRe
    Imre Palik, Nov 9, 2005
    #1
    1. Advertising

  2. Imre Palik

    Sumit Rajan Guest

    From: "Imre Palik" <>
    Newsgroups: comp.lang.c++
    Sent: Thursday, November 10, 2005 12:16 AM
    Subject: pointer to member function


    > Hi'yal,
    >
    > consider this code:
    >
    > class A
    > {
    > class B
    > {
    > void (A::*mptr)();
    > public:
    > B();
    > };
    >
    > void do_something();
    > };
    >
    > void
    > A::do_something() {}
    >
    > A::B::B() : mptr(do_something) {} // error here


    You could try replacing the above line with:
    A::B::B():mptr(&A::do_something){}

    Regards,
    Sumit.
    --
    Sumit Rajan <>
    Sumit Rajan, Nov 9, 2005
    #2
    1. Advertising

  3. Imre Palik

    Sumit Rajan Guest

    "Imre Palik" <> wrote in
    message news:...
    > Hi'yal,
    >
    > consider this code:
    >
    > class A
    > {
    > class B
    > {
    > void (A::*mptr)();
    > public:
    > B();
    > };
    >
    > void do_something();
    > };
    >
    > void
    > A::do_something() {}
    >
    > A::B::B() : mptr(do_something) {} // error here
    >
    > trying to compile it with gcc gives the following error:
    >
    > m.cpp:17: error: argument of type `void (A::)()' does not match `void
    > (A::*)()'
    >
    > I fail to see the difference between these two types. Anybody cares to
    > explain? Changing the problematic line to
    >
    > A::B::B() : mptr(&do_something) {} // error here
    >
    > I get the following error message:
    >
    > m.cpp:17: error: ISO C++ forbids taking the address of a bound member
    > function to form a pointer to member function. Say `&A::do_something'



    Sorry. I did not read this part before my previous post.

    However,
    A::B::B():mptr(&A::do_something){}


    compiles with Comeau C++ and with VC++.

    Regards,
    Sumit.
    --
    Sumit Rajan <>
    Sumit Rajan, Nov 9, 2005
    #3
  4. Imre Palik

    Sumit Rajan Guest

    "Sumit Rajan" <> wrote in message
    news:...
    >
    > "Imre Palik" <> wrote in
    > message news:...
    > However,
    > A::B::B():mptr(&A::do_something){}
    >
    >
    > compiles with Comeau C++ and with VC++.



    And with g++ 3.4.2. Which version are you using?

    Regards,
    Sumit.
    --
    Sumit Rajan <>
    Sumit Rajan, Nov 9, 2005
    #4
  5. Imre Palik

    Imre Palik Guest

    "Sumit Rajan" <> writes:

    > "Sumit Rajan" <> wrote in message
    > news:...
    > >
    > > "Imre Palik" <> wrote in
    > > message news:...
    > > However,
    > > A::B::B():mptr(&A::do_something){}
    > >
    > >
    > > compiles with Comeau C++ and with VC++.

    >
    >
    > And with g++ 3.4.2. Which version are you using?


    I know that it compiles. I just want to know why doesn't it compile
    without the A:: part. AFAIK do_something() should be in scope in a method
    of an embedded class. Or am I missing something?

    ImRe
    Imre Palik, Nov 9, 2005
    #5
  6. >
    > I know that it compiles. I just want to know why doesn't it compile
    > without the A:: part. AFAIK do_something() should be in scope in a method
    > of an embedded class. Or am I missing something?
    >


    Yes, the syntax of C++. The expression &T::f is a pointer to member. No
    other syntax will do, not T::f or &(T::f) or just plain f. Scope is
    irrelevant. Just one of those things.

    john
    John Harrison, Nov 9, 2005
    #6
  7. Imre Palik wrote:
    > ...
    > I know that it compiles. I just want to know why doesn't it compile
    > without the A:: part. AFAIK do_something() should be in scope in a method
    > of an embedded class. Or am I missing something?
    > ...


    Scope is completely irrelevant here. In C++ the one and only way to obtain a
    pointer to member is to use '&' operator explicitly and to specify a qualified
    name of the member: '&A::do_something'.

    Neither 'do_something' nor 'A::do_something' is the correct way to do it.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 9, 2005
    #7
  8. Imre Palik

    Imre Palik Guest

    John Harrison <> writes:

    > >
    > > I know that it compiles. I just want to know why doesn't it compile without the A::
    > > part. AFAIK do_something() should be in scope in a method
    > > of an embedded class. Or am I missing something?
    > >

    >
    > Yes, the syntax of C++. The expression &T::f is a pointer to member. No other syntax will
    > do, not T::f or &(T::f) or just plain f. Scope is irrelevant. Just one of those things.


    Could you point me to the relevant part of the standard?
    I tried quite hard, but I can't find it.

    Thx

    ImRe
    Imre Palik, Nov 10, 2005
    #8
  9. >
    > Could you point me to the relevant part of the standard?
    > I tried quite hard, but I can't find it.
    >


    I know the feeling. It's 5.3.1 para 3. The important part is the mention
    of 'qualified-id', i.e. you must include the class name.

    john
    John Harrison, Nov 10, 2005
    #9
  10. Imre Palik

    Imre Palik Guest

    John Harrison <> writes:

    > >
    > > Could you point me to the relevant part of the standard?
    > > I tried quite hard, but I can't find it.
    > >

    >
    > I know the feeling. It's 5.3.1 para 3. The important part is the mention of
    > 'qualified-id', i.e. you must include the class name.


    Thanks

    ImRe
    Imre Palik, Nov 11, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    594
    John Carson
    Jul 30, 2003
  2. slide_o_mix
    Replies:
    0
    Views:
    411
    slide_o_mix
    Oct 15, 2003
  3. Alex
    Replies:
    0
    Views:
    382
  4. Fraser Ross
    Replies:
    4
    Views:
    1,027
    Fraser Ross
    Aug 14, 2004
  5. somenath
    Replies:
    2
    Views:
    149
    somenath
    Aug 29, 2013
Loading...

Share This Page