I've been butting my head against this problem in an assignment I've been working on, and can't seem to get it to work at all. I wrote a little test class to demonstrate what I'm trying to do, and hopefully someone can explain what I need to do.
//Tester class
#include <iostream>
using namespace std;
template <typename T>
class Tester
{
typedef void (Tester<T>::*FcnPtr)(T);
private:
T data;
void displayThrice(T);
void doFcn( FcnPtr fcn );
public:
Tester( T item = 3 );
void function();
};
template <typename T>
inline Tester<T>::Tester( T item )
: data(item)
{}
template <typename T>
inline void Tester<T>::doFcn( FcnPtr fcn )
{
//fcn should be a pointer to displayThrice, which is then called with the class data
fcn( this->data );
}
template <typename T>
inline void Tester<T>::function()
{
//call doFcn with a function pointer to displayThrice()
this->doFcn( &Tester<T>::displayThrice );
}
template <typename T>
inline void Tester<T>::displayThrice(T item)
{
cout << item << endl;
cout << item << endl;
cout << item << endl;
}
-and here's main:
#include <iostream>
#include "Tester.h"
using namespace std;
int main()
{
Tester<int> test;
test.function();
cin.get();
return 0;
}
-and lastly, my compiler errors (VS2010)
c:\users\name\documents\visual studio 2010\projects\example\example\tester.h(28): error C2064: term does not evaluate to a function taking 1 arguments
1> c:\users\name\documents\visual studio 2010\projects\example\example\tester.h(26) : while compiling class template member function 'void Tester<T>::doFcn(void (__thiscall Tester<T>::* )(T))'
1> with
1> [
1> T=int
1> ]
1> c:\users\name\documents\visual studio 2010\projects\example\example\tester.h(21) : while compiling class template member function 'Tester<T>::Tester(T)'
1> with
1> [
1> T=int
1> ]
1> c:\users\name\documents\visual studio 2010\projects\example\example\example.cpp(7) : see reference to class template instantiation 'Tester<T>' being compiled
1> with
1> [
1> T=int
1> ]
Hopefully, my comments in the Tester class will tell you what I'm trying to do. Thank you for taking the time to look at this!
Using a pointer-to-member-function to call a function Calling the member function on an object using a pointer-to-member-function result = (object. *pointer_name)(arguments); or calling with a pointer to the object result = (object_ptr->*pointer_name)(arguments);
The pointer to member operators . * and ->* are used to bind a pointer to a member of a specific class object. Because the precedence of () (function call operator) is higher than . * and ->* , you must use parentheses to call the function pointed to by ptf .
When the name of a member template specialization appears after . or -> in a postfix-expression, or after nested-name-specifier in a qualified-id, and the postfix-expression or qualified-id explicitly depends on a template-parameter (14.6. 2), the member template name must be prefixed by the keyword template .
In C++ , function pointers when dealing with member functions of classes or structs, it is invoked using an object pointer or a this call. We can only call members of that class (or derivatives) using a pointer of that type as they are type safe.
You're not calling the member function pointer corrently; it requires the use of a special operator called the pointer-to-member operator.
template <typename T>
inline void Tester<T>::doFcn( FcnPtr fcn )
{
(this->*fcn)( this->data );
// ^^^
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With