intendi una cosa del genere? (ho adattato il tuo codice con i i funtori std)
Osserva anche che, dato che c'ero, ho messo i distruttori virtual.
codice:
#include <string>
#include <map>
#include <functional>
using namespace std;
class CBase
{
public:
CBase(void);
virtual ~CBase(void);
string name ;
string Lastname;
virtual void call(CBase b);
virtual void call2(CBase b);
};
#include <string>
using namespace std;
CBase::CBase(void)
{
}
CBase::~CBase(void)
{
}
class CProva:
public CBase
{
public:
CProva(void);
virtual ~CProva(void);
};
void CBase::call(CBase b)
{
int i = 0;
i= 1;
}
void CBase::call2(CBase b)
{
int i = 0;
i= 1;
}
int main()
{
typedef std::mem_fun1_t <void, CBase, CBase> MyMemberFunctor1Arg;
MyMemberFunctor1Arg aMemFunCall = std::mem_fun (&CBase::call);
MyMemberFunctor1Arg aMemFunCall2 = std::mem_fun (&CBase::call2);
std::map<std::string, MyMemberFunctor1Arg > m1;
m1.insert (pair<std::string,MyMemberFunctor1Arg>("giugio",aMemFunCall));
m1.insert (pair<std::string,MyMemberFunctor1Arg>("giugio2",aMemFunCall2));
CBase y;
y.name = "giuseppe";
y.Lastname = "ferrari";
CBase p;
p.name = "pippo";
p.Lastname ="giugio";
// (y.*my_memfunc_ptr)(p);
aMemFunCall (&y, p);
// Map the functions to the names
/*m["foo"] = &foo;
m["bar"] = &bar;*/
// Display all of the mapped functions
/*std::map<std::string, std::string (*)()>::const_iterator it = m.begin();
std::map<std::string, std::string (*)()>::const_iterator end = m.end();*/
std::map<std::string, MyMemberFunctor1Arg>::const_iterator it = m1.begin();
std::map<std::string, MyMemberFunctor1Arg>::const_iterator end = m1.end();
while ( it != end ) {
MyMemberFunctor1Arg der = it->second;
// (y.*der)(p);//--> richiamo la funzione
// (y.*der)(y);//--> richiamo la funzione
der (&y, p);
der (&y, y);
++it;
}
}