Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Pointer to member function of template class? (C++)

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!

like image 542
TNTisCOOL Avatar asked Oct 17 '11 20:10

TNTisCOOL


People also ask

How do you call a pointer to a member function?

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

Which pointer is used in pointer to member function?

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 .

How do you call a member function of template class?

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 .

Is it possible to create a pointer to member function of any class?

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.


1 Answers

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 );
    //   ^^^
}
like image 166
Adam Maras Avatar answered Sep 30 '22 13:09

Adam Maras