function pointer syntax in a template class
I have been experimenting with function pointers and I am not able to get
the syntax right. The following code
works properly when I execute the functions that are being pointed to from
the class that contains the
function. But I cannot get it to work when calling the function from the
class that actually holds the
pointer. What is the proper syntax. If I can figure this out I can
probably simplify my real code
significantly.
This is a console app that has a template class that holds a function
pointer. The function pointer class has
a non-template base class to allow a collection of the function pointers. A
class, UseFunctions, contains a
collection of the function pointers. The functions are in classes that
derive from the UseFunctions. The
derived classes can iterate through the function pointers with no problem.
But if I try to use the code that is commented out, the code cannot be
compiled.
Thanks for any help,
Dick
////////// File: Main.cpp /////////////////////////
#include <iostream>
using namespace std;
#include "FunctionPointer.h"
int main ()
{
HasFunction1 fn1;
cout << fn1.Function1 ("test 1") << endl << endl;
fn1.ExecuteFunctions ();
//fn1.ExecuteFunction (1);
HasFunction2 fn2;
fn2.ExecuteFunctions ();
return 0;
}
////////// File: FunctionPointer.h /////////////////////////
#pragma once
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class FunctionPointerBase
{
public:
FunctionPointerBase () {};
virtual ~FunctionPointerBase () {};
//virtual string ExecuteFunction (string s) = 0;
};
template <class T>
class FunctionPointer : public FunctionPointerBase
{
public:
string (__thiscall T::*theFunction) (string);
FunctionPointer () : theFunction (0) {};
FunctionPointer (string (__thiscall T::*function) (string))
: theFunction (function) {};
~FunctionPointer () {};
//string ExecuteFunction (string s)
//{
////////// What is the proper syntax for this line??????????
// string result = (*T.*theFunction) (s);
// //string result = (*this.*theFunction) (s);
// return result;
//}
};
class UseFunctions
{
public:
vector <FunctionPointerBase*> functions;
UseFunctions () {};
~UseFunctions () {};
virtual void ExecuteFunctions () = 0;
//string ExecuteFunction (int index)
//{
// functions [index]->ExecuteFunction ("access specific function");
//}
};
class HasFunction1 : public UseFunctions
{
public:
HasFunction1 ()
{
functions.push_back
(new FunctionPointer <HasFunction1> (&HasFunction1::Function1));
functions.push_back
(new FunctionPointer <HasFunction1> (&HasFunction1::Function2));
};
virtual ~HasFunction1 () {};
string Function1 (string s)
{
return "In HasFunction1::Function1: " + s;
};
string Function2 (string s)
{
return "In HasFunction1::Function2: " + s;
};
void ExecuteFunctions ()
{
vector <FunctionPointerBase*>::iterator i = functions.begin ();
while (i != functions.end ())
{
FunctionPointer <HasFunction1>* fn =
dynamic_cast <FunctionPointer <HasFunction1>*> (*i++);
string result = (*this.*(fn->theFunction)) ("testing");
cout << result << endl;
}
};
};
class HasFunction2 : public UseFunctions
{
public:
HasFunction2 ()
{
functions.push_back
(new FunctionPointer <HasFunction2> (&HasFunction2::Function1));
functions.push_back
(new FunctionPointer <HasFunction2> (&HasFunction2::Function2));
};
virtual ~HasFunction2 () {};
string Function1 (string s)
{
return "In HasFunction2::Function1: " + s;
};
string Function2 (string s)
{
return "In HasFunction2::Function2: " + s;
};
void ExecuteFunctions ()
{
vector <FunctionPointerBase*>::iterator i = functions.begin ();
while (i != functions.end ())
{
FunctionPointer <HasFunction2>* fn =
dynamic_cast <FunctionPointer <HasFunction2>*> (*i++);
string result = (*this.*(fn->theFunction)) ("testing");
cout << result << endl;
}
};
};