how to pass function pointer as arg in c++
Hello all sorry for the bad english
how can i pass function pointer as function arg
let me explain :
i have Class A inside one of its methods lts call it A::a i start 3 worker threads
that i initiate in side this class and pass the worker thread to thread pool.
this worker thread need to invoke Class A method called A::b.
my question is how can i sent function pointer of A::b to the worker thread
the worker thread class will get it in the constructor.
or there is some other way to do it
i fallow the example from :http://www.parashift.com/c++-faq-lit....html#faq-33.7
but then i had problem until i try to pass the member argument as another arg.
here is sample code that dose not work
Code:
TestMethod.cpp
---------------------------------------
#include "TestMethod.h"
#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
void callit(TestClass& o, FredMemFn p,std::vector<std::string>* vv)
{
CALL_MEMBER_FN(o,p)(vv);
}
TestMethod::TestMethod()
{;}
void TestMethod::Init(TestClass& o, FredMemFn p,std::vector<std::string>* vv1)
{
int h = vv1->size();
callit(o,p,vv1);
}
TestMethod.h
--------------------------------------
class TestClass;
#include <vector>
typedef void (TestClass::*FredMemFn)(std::vector<std::string>* vv);
class TestMethod
{
public :
TestMethod();
void Init(TestClass& o, FredMemFn p,std::vector<std::string>* vv);
private :
TestClass* m_TestClass;
};
TestClass.cpp
----------------------------------------
#include "TestClass.h"
#include "TestMethod.h"
#include <iostream>
#include <stdio.h>
using namespace std;
typedef void (TestClass::*FredMemFn)(std::vector<std::string>* vv);
TestClass::TestClass()
{;}
TestClass::~TestClass()
{;}
void TestClass::SayHelp(std::vector<std::string>* vv){
cout << "Help" <<endl;
}
void TestClass::add()
{
std::vector<std::string> vv1;
vv1.push_back("DDDD");
TestMethod* testMethod = new TestMethod();
FredMemFn p = &TestClass::SayHelp;
testMethod->Init(*this,p,&vv1);
}
TestClass.h
----------------------------------------
#include <vector>
class TestClass
{
public :
TestClass();
~TestClass();
void add();
void SayHelp(std::vector<std::string>* vv1);
private:
int m_test;
};
thank for helping