C++ template design with function pointers - c++

Im bit new to c++, please let me describe my problem. I have a class called CSubcriber, which main purpose is to execute some callback function, of some type and some number of arguments. For example callbacks can be:
typedef void (*cb1)(void)
typedef int (*cb2)(int, int)
typedef char (*cb3)(int, int, void);
class itself is:
template <class CallbackType>
class CSubscriber {
public:
CSubscriber();
virtual ~CSubscriber();
CallbackType (*m_callback_newWay)(void *params);
void (*m_callback)(void *params);
int (*m_callback_int)(void *params);
void *m_cb_params;
int execute_cb();
void *m_params;
CSubscriber(void (*callback)(void *params), void *cb_params);
CSubscriber(int (*callback)(void *params), void *cb_params);
};
CSubscriber::CSubscriber(void (*callback)(void *params), void *cb_params){
m_callback = callback;
m_cb_params = cb_params;
}
CSubscriber::CSubscriber(int (*callback)(void *params), void *cb_params){
m_callback_int = callback;
m_cb_params = cb_params;
}
My main problem is now, how to write constructor, which will handle with all that variable arguments to callback. After constructing object in some particular moment, callback may be fired, for example:
typedef int (*callback)(void *params);
CSubscriber s(callback, params);
or
typedef void (*callback2)(void *params, int arr, char *ptr);
CSubscriber s(callback, params, arr, ptr);
Now, calling
s->m_callback()
I want to execute callback with all arguments which i passed for constructor.
I'd like to avoid writing ten different constructors, for each different number of arguments passed to callback function. Is it possible to do ?

Try something like this.
#include <iostream>
using namespace std;
typedef void (*cb1)(void);
typedef int (*cb2)(int, int);
typedef char (*cb3)(int, int, char);
template <class CallbackType>
class CSubscriber
{
public:
CSubscriber(): fcn1(0), fcn2(0), fcn3(0) {};
virtual ~CSubscriber(){};
CSubscriber(cb1 fcn): fcn1(fcn), fcn2(0), fcn3(0), a(), b() {};
CSubscriber(cb2 fcn, int p1, int p2): fcn1(0), fcn2(fcn), fcn3(0), a(p1), b(p2) {};
int execute_cb() {
if ( fcn1 != 0 ) {
(*fcn1)();
}
if ( fcn2 != 0 ) {
(*fcn2)(a,b);
}
};
protected:
cb1 fcn1;
cb2 fcn2;
cb3 fcn3;
int a, b;
};
void fcn1() {
cout << "in fcn1" << endl;
};
int fcn2(int a, int b) {
cout << "in fcn2, a " << a << ", b " << b << endl;
};
int main()
{
CSubscriber<int> cs;
CSubscriber<int> cs1(&fcn1);
CSubscriber<int> cs2(&fcn2, 1, 100);
cs.execute_cb();
cs1.execute_cb();
cs2.execute_cb();
}

Related

passing function as argument in c++

I am trying pass the function as an argument, the Testabc is inherited from MainTest and the function I want to pass is protected function in MainTest class. I do not have the cpp access to the MainTest class which has this protected ReadTestPoint function.
Below is header file where I define the function that is taking the function as an argument.
#include <QObject>
#include <QDebug>
class TestManager
{
public:
TestManager();
~TestManager() {}
int ReadTestPointer(void *dp, unsigned int &val, int (*functioncall)(void *,
unsigned int&));
};
Below is the cpp for the TestManager
#include "testmanager.h"
#include<QDebug>
TestManager::TestManager(){}
int TestManager::ReadTestPointer(void* dp, unsigned int &num, int (*readt)
(void*, unsigned int&))
{
qDebug()<< "Function Pointer working";
int g;
g = (*readt)(dp, num);
return g;
}
The class from where I am making the call:
namespace PackageCore
{
TestAbc::TestAbc() : MainTest(){}
TestAbc::~TestAbc(){}
int TestAbc::Init()
{
// initialization code called once
m_config = reinterpret_cast<Test_BaseClass*>
(GetConfig(Test_BaseClass_INTERFACE_HASH));
return 0;
}
int TestAbc::DeInit()
{
return 0;
}
int TestAbc::Cycle()
{
TestManager m_TestManager;
unsigned int m_trigger;
int (*abc)(void *, unsigned int&) = ReadTestPoint(m_config-
>SHM_B_Trigger_U8, m_trigger);
m_TestManager.ReadTestPointer(m_config->SHM_B_Trigger_U8, m_trigger, abc);
qDebug()<< " getTrigger: " << m_trigger;
return 0;
}
}
But I get the compile time error on this:
C:\test_manager_git\testabc.cpp:39: error: invalid conversion from 'int' to 'int (*)(void*, unsigned int&)' [-fpermissive]
int (*abc)(void *, unsigned int&) = ReadTestPoint(m_config->SHM_B_Trigger_U8, m_trigger);
The MainTest.h is below:
class MainTest : public QObject
{
Q_OBJECT
public:
// Callbacks
virtual int Init() = 0;
virtual int Cycle() = 0;
virtual int DeInit() = 0;
protected:
int ReadTestPoint (void *dp, unsigned int &val);
};
Thanks
First of all, consider using things like std::function instead of rolling your own pointer nightmare. But lets get started...
Basically, in order to call a member function from pointer, you need the function pointer and a member instance. The following code is based on your question code with the added member pointer.
#include <iostream>
class MainTest
{
public:
protected:
int ReadTestPoint (void *dp, unsigned int &val)
{
std::cout << "protected ReadTestPoint called" << std::endl;
return 0;
}
};
class TestManager
{
public:
TestManager() {}
~TestManager() {}
int ReadTestPointer(void *dp, unsigned int &val, MainTest* instance, int (MainTest::*functioncall)(void *, unsigned int&))
{
return (instance->*functioncall)(dp, val);
}
};
class TestAbc : public MainTest
{
public:
void ExecTest()
{
TestManager testManager;
unsigned int tVal;
void* dummy = &tVal;
testManager.ReadTestPointer(dummy, tVal, this, &TestAbc::ReadTestPoint);
}
};
int main(void)
{
TestAbc test;
test.ExecTest();
return 0;
}
If you don't want to restrict yourself to a specific member type, consider using a template function:
class TestManager
{
public:
TestManager() {}
~TestManager() {}
template<typename Fn>
int ReadTestPointer(void *dp, unsigned int &val, Fn functioncall)
{
return functioncall(dp, val);
}
};
It will accept non-member functions and objects that overload the operator() with appropriate parameters and return types.
You can wrap the member function pointer in a Functor object:
template<typename TMember, typename TResult, typename TParam1, typename TParam2>
struct Functor
{
typedef TResult (TMember::*TFn)(TParam1, TParam2);
Functor(TMember* m, TFn func):member(m), fn(func){}
TMember* member;
TFn fn;
TResult operator()(TParam1 p1, TParam2 p2)
{
return (member->*fn)(p1, p2);
}
};
The following example includes a free function call and a member function call:
int FreeFn(void *dp, unsigned int &val)
{
std::cout << "free function called" << std::endl;
return 1;
}
class TestAbc : public MainTest
{
public:
void ExecTest()
{
TestManager testManager;
unsigned int tVal;
void* dummy = &tVal;
testManager.ReadTestPointer(dummy, tVal, Functor<TestAbc, int, void*, unsigned int&>(this, &TestAbc::ReadTestPoint));
testManager.ReadTestPointer(dummy, tVal, FreeFn);
}
};

How to use different functoids within an array or vector

I have written a small piece of code where I am able to call setter and getter functions packed within a functoid using mem_fun templates.
I now would like to use this approach on top of a class hierarchy where every class might have getter and setter which can be registered as pair within a vector or array to be able to call the getter and setter if needed. GUIObject and GUICompositeObject are example classes out of the described class hierarchy.
The bound_mem_fun_t for the objects have unfortunately different types and thats the reason I don't know how to integrate them into an array/vector of pointers to the functors.
In c++11 I would use std::function. Is there a way to emulate this in c++98?
Because our compiler support only c++98 I cannot use the new features of c++11 or c++14. Also boost is not allowed.
#include <functional>
class GUIObject
{
int m_Alpha;
public:
void SetAlpha(int a) { m_Alpha = a;};
int GetAlpha() {return m_Alpha;};
};
class GUICompositeObject: public GUIObject
{
int m_NumOfChilds;
public:
void SetNumOfChilds(int NumOfChilds) { m_NumOfChilds = NumOfChilds;};
int GetNumOfChilds() {return m_NumOfChilds;};
};
template<typename T>
struct bound_mem_fun_t
{
bound_mem_fun_t(std::mem_fun_t<int, T> GetFunc, std::mem_fun1_t<void, T, int> SetFunc, T* o) :
m_GetFunc(GetFunc), m_SetFunc(SetFunc), obj(o) { } ;
int operator()() { return m_GetFunc(obj); } ;
void operator()(int i) { m_SetFunc(obj, i); } ;
std::mem_fun_t<int, T> m_GetFunc;
std::mem_fun1_t<void, T, int> m_SetFunc;
T* obj;
};
int main()
{
GUIObject kGUIObject;
GUICompositeObject kCompObj;
bound_mem_fun_t<GUIObject> GUIObjectFunc(std::mem_fun(&GUIObject::GetAlpha), std::mem_fun(&GUIObject::SetAlpha), &kGUIObject);
GUIObjectFunc(17);
int ii = GUIObjectFunc();
bound_mem_fun_t<GUICompositeObject> GUICompObjectFunc(std::mem_fun(&GUICompositeObject::GetNumOfChilds), std::mem_fun(&GUICompositeObject::SetNumOfChilds), &kCompObj);
GUICompObjectFunc(17);
int iChilds = GUICompObjectFunc();
return 0;
}
Here is the complete solution after #filmors answer:
#include <functional>
#include <vector>
#include <iostream>
class GUIObject
{
int m_Alpha;
public:
void SetAlpha(int a) { m_Alpha = a;};
int GetAlpha() {return m_Alpha;};
};
class GUICompositeObject: public GUIObject
{
int m_NumOfChilds;
public:
void SetNumOfChilds(int NumOfChilds) { m_NumOfChilds = NumOfChilds;};
int GetNumOfChilds() {return m_NumOfChilds;};
};
struct bound_mem_fun_base
{
virtual int operator()() =0;
virtual void operator()(int) =0;
};
template<typename T>
struct bound_mem_fun_t : public bound_mem_fun_base
{
bound_mem_fun_t(std::mem_fun_t<int, T> GetFunc, std::mem_fun1_t<void, T, int> SetFunc, T* o) :
m_GetFunc(GetFunc), m_SetFunc(SetFunc), obj(o) { } ;
virtual int operator()() { return m_GetFunc(obj); } ;
virtual void operator()(int i) { m_SetFunc(obj, i); } ;
std::mem_fun_t<int, T> m_GetFunc;
std::mem_fun1_t<void, T, int> m_SetFunc;
T* obj;
};
template<typename T> bound_mem_fun_t<T>* make_setter(std::mem_fun_t<int, T> GetFunc, std::mem_fun1_t<void, T, int> SetFunc, T* o)
{
return new bound_mem_fun_t<T> (GetFunc, SetFunc, o);
}
int main()
{
GUIObject kGUIObject;
GUICompositeObject kCompObj;
std::vector<bound_mem_fun_base*> kBoundVector;
kBoundVector.push_back(new bound_mem_fun_t<GUIObject> (std::mem_fun(&GUIObject::GetAlpha), std::mem_fun(&GUIObject::SetAlpha), &kGUIObject));
kBoundVector.push_back(new bound_mem_fun_t<GUICompositeObject> (std::mem_fun(&GUICompositeObject::GetNumOfChilds), std::mem_fun(&GUICompositeObject::SetNumOfChilds), &kCompObj));
kBoundVector.push_back(make_setter<GUIObject> (std::mem_fun(&GUIObject::GetAlpha), std::mem_fun(&GUIObject::SetAlpha), &kGUIObject));
kBoundVector.push_back(make_setter<GUICompositeObject> (std::mem_fun(&GUICompositeObject::GetNumOfChilds), std::mem_fun(&GUICompositeObject::SetNumOfChilds), &kCompObj));
for (int i = 0; i < 4 ; i++)
{
(*kBoundVector[i])(i*10);
int res = (*kBoundVector[i])();
std::cout << "Getter result " << res << "\n";
}
return 0;
}
Unfortunately the make_setter function does not really shorten the creation of the functor. Any ideas will be welcome.
Just give your bound_mem_fun_t<T> a common base class and use dynamic dispatch to solve your problem:
struct bound_mem_fun_base {
virtual int operator()() = 0;
virtual void operator()(int) = 0;
};
template <typename T>
struct bound_mem_fun_t : bound_mem_fun_t ...
Then you can keep pointers to bound_mem_fun_base in your vector and call the elements as (*v[0])().
Also, TR1 does contain std::tr1::function, is that available?
First a remark on std::function from c++11: That will not solve your problem, because you need an already bounded function pointer. This pointer must be bound to your object. I believe what you need is an own implementation to std::bind.
I started only a very! small Binder class which is hopefully a starting point for your needs. If you need to have template parameter lists in older c++ versions, take a look for loki. http://loki-lib.sourceforge.net/
As a hint I can give you a short example of what i did:
class A
{
private:
int val;
public:
A(int i): val(i) {}
void Do(int i) { std::cout << "A " << val<< " " << i << std::endl; }
};
class B
{
private:
int val;
public:
B(int i): val(i){}
void Go(int i) { std::cout << "B " << val << " " << i << std::endl; }
};
class Base
{
public:
virtual void operator()(int i)=0;
};
template <typename T>
class Binder: public Base
{
void (T::*fnct)(int);
T* obj;
public:
Binder( void(T::*_fnct)(int), T*_obj):fnct(_fnct),obj(_obj){}
void operator()(int i)
{
(obj->*fnct)(i);
}
};
int main()
{
A a(100);
B b(200);
// c++11 usage for this example
//std::function<void(int)> af= std::bind( &A::Do, &a, std::placeholders::_1);
//af(1);
// hand crafted solution
Base* actions[2];
actions[0]= new Binder<A>( &A::Do, &a);
actions[1]= new Binder<B>( &B::Go, &b);
actions[0]->operator()(55);
actions[1]->operator()(77);
}

Call different versions of template member function based on template paramaters

I need to call different versions of a template member function with the same arguments based on certain static members of the template parameters. Here's a sort of simplified version of what I need to do:
class A {
public:
//...
static const char fooString[];
};
const char A::fooString[] = "This is a Foo.";
class B {
public:
//...
static const char barString[];
};
const char B::barString[] = "This is a Bar.";
class C {
public:
//...
static const char fooString[];
};
const char C::fooString[] = "This is also a Foo.";
//Many other classes which have either a fooString or a barString
void doFoo(const char*s) { /*something*/ }
void doBar(const char*s) { /*something else*/ }
template&ltclass T>
class Something {
public:
//This version should be called if T has a static member called "fooString",
//so it should be called if T is either class A or C
void doSomething() { doFoo(T::fooString); }
//This version should be called if T has a static member called "barString",
//so it should be called if T is class B
void doSomething() { doBar(T::barString); }
};
void someFunc()
{
Something&ltA> a;
Something&ltB> b;
Something&ltC> c;
a.doSomething(); //should call doFoo(A::fooString)
b.doSomething(); //should call doBar(B::barString)
c.doSomething(); //should call doFoo(C::fooString)
}
How would I achieve this?
A possible solution:
#include <iostream>
#include <type_traits>
class A {
public:
//...
static const char fooString[];
};
const char A::fooString[] = "This is a Foo.";
class B {
public:
//...
static const char barString[];
};
const char B::barString[] = "This is a Bar.";
class C {
public:
//...
static const char fooString[];
};
const char C::fooString[] = "This is also a Foo.";
void doFoo(const char*s) { std::cout << "doFoo: " << s << "\n"; }
void doBar(const char*s) { std::cout << "doBar: " << s << "\n"; }
template<class T>
class Something {
public:
//This version should be called if T has a static member called "fooString",
//so it should be called if T is either class A or C
template <typename TT = T, typename std::enable_if<TT::fooString != 0, bool>::type = false>
void doSomething() { doFoo(T::fooString); }
//This version should be called if T has a static member called "barString",
//so it should be called if T is class B
template <typename TT = T, typename std::enable_if<TT::barString != 0, bool>::type = false>
void doSomething() { doBar(T::barString); }
};
int main()
{
Something<A> a;
Something<B> b;
Something<C> c;
a.doSomething(); //should call doFoo(A::fooString)
b.doSomething(); //should call doBar(B::barString)
c.doSomething(); //should call doFoo(C::fooString)
}
Output:
doFoo: This is a Foo.
doBar: This is a Bar.
doFoo: This is also a Foo.

ARG! Contravarience foils callback plot! Want to change virtual return type

I am working on a AI dynamic link library. It is to be explicitly linked and all of this is handled by a simple header include.
I am at the moment trying to create the code for the DLL to be able to call functions in the main EXE to manipulate the world and also to be able to query functions to learn about the state of the world. I am now at the point where I can call void return functions (be they global functions or member functions) with no parameters.
I am now trying to implement the ability to call functions in the EXE and get the return value from them. (non-void functions) Things are not going well.. I've been trying for a while now trying to find the best way to implement this. I can use boost libraries in the DLL but not in the EXE.
I'm going to do a dump of the relavent code here. I know it is alot but I hope someone will be able to point out how I can improve.
Even if you do not read the code (very understandable) it would be very helpful to know how you would go about tackling this problem in general terms.
Here goes (I've tried to cut out as many unrelated sections of code as I can):
---------------------------------------EXE side header--------------------------------------
typedef void (*Command)();
typedef void (*CommandMF)(int, std::string);
typedef void (*AddEntityFunction)(int&);
typedef void (*AddActionToEntityFunction)(int, Command);
typedef void (*AddActionToEntityFunctionMF)(int, CommandMF, std::string);
typedef void (*UpdateDoubleThinkFunction)();
class MemberFunctionStorageExecute
{
public:
virtual void Execute() const =0;
};
template <class T>
struct MemberFunctionStorage : MemberFunctionStorageExecute
{
typedef void (T::*MemberFunctionWorker)();
MemberFunctionWorker mbw;
T *obj;
virtual void Execute() const
{
(obj->*mbw)();
}
};
typedef std::map<std::string, MemberFunctionStorageExecute*> MemberFunctionsList;
typedef std::map<int, MemberFunctionsList*> ListofLists;
//Template hack to allow static properties inside header
template <class T>
class DoubleThinkInterfaceImpl
{
protected:
static HINSTANCE hinstDLL;
static AddEntityFunction DLLAddEntity;
static AddActionToEntityFunction DLLAddActionToEntity;
static AddActionToEntityFunctionMF DLLAddActionToEntityMF;
static UpdateDoubleThinkFunction DLLUpdateDoubleThink;
static ListofLists m_plistlist;
};
template <class T>
HINSTANCE DoubleThinkInterfaceImpl<T>::hinstDLL;
template <class T>
AddEntityFunction DoubleThinkInterfaceImpl<T>::DLLAddEntity;
template <class T>
UpdateDoubleThinkFunction DoubleThinkInterfaceImpl<T>::DLLUpdateDoubleThink;
template <class T>
AddActionToEntityFunction DoubleThinkInterfaceImpl<T>::DLLAddActionToEntity;
template <class T>
AddActionToEntityFunctionMF DoubleThinkInterfaceImpl<T>::DLLAddActionToEntityMF;
template <class T>
ListofLists DoubleThinkInterfaceImpl<T>::m_plistlist;
class DoubleThinkInterface : protected DoubleThinkInterfaceImpl<int>
{
private:
int m_pid;
MemberFunctionsList m_pmemfunlist;
public:
int ID()
{
return m_pid;
}
DoubleThinkInterface()
{
if(!hinstDLL)
{
hinstDLL = LoadLibrary("DoubleThink.dll");
DLLAddEntity = (AddEntityFunction)GetProcAddress(hinstDLL, "AddEntity");
DLLUpdateDoubleThink = (UpdateDoubleThinkFunction)GetProcAddress(hinstDLL, "Update");
DLLAddActionToEntity = (AddActionToEntityFunction)GetProcAddress(hinstDLL, "AddActionToEntity");
DLLAddActionToEntityMF = (AddActionToEntityFunctionMF)GetProcAddress(hinstDLL, "AddActionToEntityMF");
}
DLLAddEntity(m_pid);
DoubleThinkInterface::m_plistlist.insert(std::pair<int, MemberFunctionsList*>(m_pid, &m_pmemfunlist));
}
~DoubleThinkInterface()
{
//if(hinstDLL != 0)
// FreeLibrary(hinstDLL);
}
void AddAction(Command action)
{
DLLAddActionToEntity(m_pid, action);
}
void Update()
{
DLLUpdateDoubleThink();
}
template <class T>
void AddActionMF(T *object, void (T::*memberfunc)(), std::string actionName)
{
MemberFunctionStorage<T> *store = new MemberFunctionStorage<T>;
store->mbw = memberfunc;
store->obj = object;
m_pmemfunlist.insert(std::pair<std::string, MemberFunctionStorageExecute*>(actionName, store));
DLLAddActionToEntityMF(m_pid, &DoubleThinkInterface::ResolveMF, actionName);
}
static void ResolveMF(int idnum,std::string mfName)
{
ListofLists::iterator lit;
lit = m_plistlist.find(idnum);
MemberFunctionsList::iterator it;
it = lit->second->find(mfName);
it->second->Execute();
}
};
-------------------------------EXE-side example------------------------------------
class BaseEntity
{
public:
DoubleThinkInterface dtInterface;
BaseEntity(){}
virtual ~BaseEntity(){}
};
class Humanoid : public BaseEntity
{
public:
Humanoid(){}
~Humanoid(){}
std::string name;
void Move();
int GetAge(){return 10;}
};
void Humanoid::Move()
{
std::cout << name << ": I'm moving around and such \n";
}
void EndLifeAsWeKnowIt()
{
cout << "Suddenly everything changed... \n";
}
int _tmain(int argc, _TCHAR* argv[])
{
Humanoid *entity = new Humanoid();
entity->name = "Bobby";
entity->dtInterface.AddAction(&EndLifeAsWeKnowIt);
entity->dtInterface.AddActionMF<Humanoid>(entity, &Humanoid::Move, "Move");
entity->dtInterface.Update();
int x; cin >> x;
return 0;
}
-------------------------DLL-side code------------------------------------
DTEntityManager* DTEntityManager::Instance()
{
static DTEntityManager instance;
return &instance;
}
template<class T>
void AddAction(int id, void (*comm)(int, std::string), std::string mfid)
{
DTEntity *ent = DTEntityManager::Instance()->GetEntityFromID(id);
CommandMemberFunction<T> *newcomm = new CommandMemberFunction<T>();
newcomm->comm = comm;
newcomm->entityid = id;
newcomm->mfid = mfid;
ent->SetCommandMF(newcomm);
}
extern "C"
{
DLL_EXPORT void AddEntity(int &idnumber)
{
DTEntity *entity = new DTEntity();
idnumber = entity->ID();
DTEntityManager::Instance()->RegisterEntity(entity);
}
DLL_EXPORT void AddActionToEntity(int id, void (*comm)())
{
DTEntity *ent = DTEntityManager::Instance()->GetEntityFromID(id);
CommandGlobal<void> *newcomm = new CommandGlobal<void>();
newcomm->comm = comm;
ent->SetCommand(newcomm);
}
DLL_EXPORT void AddActionToEntityMF(int id, void (*comm)(int, std::string), std::string mfid)
{
AddAction<void>(id, comm, mfid);
}
DLL_EXPORT void AddActionToEntityMF_int(int id, void (*comm)(int, std::string), std::string mfid)
{
AddAction<int>(id, comm, mfid);
}
}
--------------------------DLL-side Structure for holding callbacks ---------------------------
class CommandBase
{
public:
virtual void Execute() const =0;
};
template<class T>
struct CommandGlobal : CommandBase
{
typedef boost::function<T ()> Command;
Command comm;
virtual T Execute() const
{
return comm();
}
};
template<class T>
struct CommandMemberFunction : CommandBase
{
typedef boost::function<T (int, std::string)> Command;
Command comm;
int entityid;
std::string mfid;
virtual T Execute() const
{
return comm(entityid, mfid);
}
};
At the moment the DLL doesn't compile because of this line:
AddAction<int>(id, comm, mfid);
Because it tries to override
virtual void Execute() const =0;
with a function which returns int. Gives non-covariance error.. I know I have been barking up the wrong tree but I can't see any other solution at the moment either.
Does anyone have any advice on how to do it better? Even if it just a vague direction I should direct my attention in I would appreciate it. Thanks a lot if you bother to read all this!
I think you are complicating too much. See this:
/*************** both ***************/
typedef void (*Prototype1)();
typedef void (*Prototype2)(int);
typedef int (*Prototype3)();
struct FuncList {
Prototype3 func1;
Prototype1 func2;
Prototype1 func3;
Prototype2 func4;
Prototype1 func5;
// ...
};
/*************** dll ***************/
FuncList func_list;
Prototype3 &func1 = func_list.func1;
Prototype1 &func2 = func_list.func2;
Prototype1 &func3 = func_list.func3;
Prototype2 &func4 = func_list.func4;
Prototype1 &func5 = func_list.func5;
/* DLLEXPORT */ void SetFuncList(const FuncList &list) { func_list = list; }
void UsageExample()
{
/* Just call the function */
func2();
}
/*************** exe ***************/
/* declarations (functions must be defined somewhere) */
int func1();
void func2();
void func3();
void func4(int);
void func5();
const FuncList func_list = {
func1,
func2,
func3,
func4,
func5
};
typedef void (*SetFuncListProc)(const FuncList &list);
SetFuncListProc SetFuncList;
void Init()
{
/* ... load the DLL, load "SetFuncList" ... */
SetFuncList(func_list);
}
Quick & dirty answer: pass to Execute a reference to the result type as a void*, and make Execute private. Then wrap Execute in a non-virtual wrapper which returns T by value and does the cast.

How do you declare a const array of function pointers?

Firstly, I've got functions like this.
void func1();
void func2();
void func3();
Then I create my typedef for the array:
void (*FP)();
If I write a normal array of function pointers, it should be something like this:
FP array[3] = {&func1, &func2, &func3};
I want to make it a constant array, using const before "FP", but I've got this error messages:
error: cannot convert 'void ( * )()' to 'void ( * const)()' inialization
PD: Sorry my bad English.
EDIT:
x.h
typedef void (*FP)();
class x
{
private:
int number;
void func1();
void func2();
void func3();
static const FP array[3];
}
x.cpp
const FP x::array[3] = {&x::func1, &x::func2, &x::func3};
My code is more large and complex, this is a summary
Then I create my typedef for the array:
void (*FP)();
Did you miss typedef before void?
Following works on my compiler.
void func1(){}
void func2(){}
void func3(){}
typedef void (*FP)();
int main()
{
const FP ar[3]= {&func1, &func2, &func3};
}
EDIT (after seeing your edits)
x.h
class x;
typedef void (x::*FP)(); // you made a mistake here
class x
{
public:
void func1();
void func2();
void func3();
static const FP array[3];
};
without typedef:
void (*const fp[])() = {
f1,
f2,
f3,
};
typedef void (*FPTR)();
FPTR const fa[] = { f1, f2};
// fa[1] = f2; You get compilation error when uncomment this line.
Which compiler are you using? This works on VS2005.
#include <iostream>
void func1() {std::cout << "func1" << std::endl;}
void func2() {std::cout << "func2" << std::endl;}
void func3() {std::cout << "func3" << std::endl;}
int main()
{
int ret = 0;
typedef void (*FP)();
const FP array[3] = {&func1, &func2, &func3};
return ret;
}
If you want the array itself to be const:
FP const a[] =
{
func1,
func2,
func3
};