how to invoke pointer-to-member-function of a class

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Aug 26, 2010.

  1. , India

    , India Guest

    The following program is only for learning purpose.

    Consider the following program x.cpp:
    #include <cstdlib>
    #include <iostream>

    using namespace std;

    template<class T>
    void fn(void (T::*memfnPtr)() const)
    {
    cout << "From fn(): calling T::*memfnPtr()" << endl;
    // The following nvocation doesn't work; what is the
    // syntax for invoking the memfnPtr() ?
    *memfnPtr();

    return;
    }

    class Test
    {
    public:
    void member() const;
    };

    inline void Test::member(void) const
    {
    cout << "Test::member() const called" << endl;

    return;
    }

    int main()
    {
    fn(&Test::member);

    return EXIT_SUCCESS;
    }

    I compiled this program with g++3.4.3 as
    g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

    I get the following compilation error:
    x.cpp: In function `void fn(void (T::*)() const) [with T = Test]':
    x.cpp:32: instantiated from here
    x.cpp:12: error: must use .* or ->* to call pointer-to-member function
    in `memfnPtr (...)'

    Kindly explain me the syntax for calling memfnPtr() inside fn() and
    help me to fix this compilation error.

    Thanks
    V.Subramanian
    , India, Aug 26, 2010
    #1
    1. Advertising

  2. * , India, on 26.08.2010 05:19:
    > The following program is only for learning purpose.


    Yes, it seems that it's not only learning (as in self-study), but HOMEWORK.



    > Consider the following program x.cpp:
    > #include<cstdlib>
    > #include<iostream>
    >
    > using namespace std;
    >
    > template<class T>
    > void fn(void (T::*memfnPtr)() const)
    > {
    > cout<< "From fn(): calling T::*memfnPtr()"<< endl;
    > // The following nvocation doesn't work; what is the
    > // syntax for invoking the memfnPtr() ?
    > *memfnPtr();
    >
    > return;
    > }
    >
    > class Test
    > {
    > public:
    > void member() const;
    > };
    >
    > inline void Test::member(void) const
    > {
    > cout<< "Test::member() const called"<< endl;
    >
    > return;
    > }
    >
    > int main()
    > {
    > fn(&Test::member);
    >
    > return EXIT_SUCCESS;
    > }
    >
    > I compiled this program with g++3.4.3 as
    > g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
    >
    > I get the following compilation error:
    > x.cpp: In function `void fn(void (T::*)() const) [with T = Test]':
    > x.cpp:32: instantiated from here
    > x.cpp:12: error: must use .* or ->* to call pointer-to-member function
    > in `memfnPtr (...)'
    >
    > Kindly explain me the syntax for calling memfnPtr() inside fn() and
    > help me to fix this compilation error.


    Just look at our compiler output.

    It tells you everything you need to know (given that you have a C++ book).


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Aug 26, 2010
    #2
    1. Advertising

  3. Venu Yanamandra, Aug 26, 2010
    #3
  4. , India

    James Kanze Guest

    On Aug 26, 4:19 am, ", India"
    <> wrote:
    > The following program is only for learning purpose.


    > Consider the following program x.cpp:
    > #include <cstdlib>
    > #include <iostream>


    > using namespace std;


    > template<class T>
    > void fn(void (T::*memfnPtr)() const)
    > {
    > cout << "From fn(): calling T::*memfnPtr()" << endl;
    > // The following nvocation doesn't work; what is the
    > // syntax for invoking the memfnPtr() ?
    > *memfnPtr();


    In order to call a (non-static) member function, you need an
    object. Where is your object?

    If you have an object, the syntax is (obj.*memfnPtr)(). If you
    have a pointer to an object, the syntax is (ptr->*memfnPtr)().
    (The type of the object must be T, of course.)

    > return;
    > }


    --
    James Kanze
    James Kanze, Aug 26, 2010
    #4
    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. christopher diggins
    Replies:
    7
    Views:
    2,057
    christopher diggins
    Feb 13, 2004
  2. Fraser Ross
    Replies:
    4
    Views:
    1,039
    Fraser Ross
    Aug 14, 2004
  3. Replies:
    12
    Views:
    720
    fluden
    Feb 11, 2005
  4. Praetorian
    Replies:
    11
    Views:
    2,346
    James Kanze
    Apr 3, 2008
  5. somenath
    Replies:
    2
    Views:
    155
    somenath
    Aug 29, 2013
Loading...

Share This Page