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