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

S

subramanian100in

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
 
A

Alf P. Steinbach /Usenet

* (e-mail address removed), 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
 
J

James Kanze

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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top