L
lallous
Hello
#include <iostream>
#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
class Fred;
typedef int (Fred::*FredMemFn)(char x, float y);
class Fred
{
private:
FredMemFn m_fn;
public:
int f(char x, float y);
int g(char x, float y);
int h(char x, float y) { std::cout << "Fred's h()" << std::endl; return
0;}
int i(char x, float y);
void SetFN(FredMemFn f) { m_fn = f; }
void carry()
{
CALL_MEMBER_FN(*this, m_fn)('x', 1);
}
};
class CMyFred : public Fred
{
public:
int x(char a, float b)
{
std::cout << "MyFred's x()" << std::endl;
}
void test();
};
void CMyFred::test()
{
// this works
SetFN(Fred::h);
// this doesn't <-- how to make it work?
SetFN(x);
}
int main()
{
CMyFred f;
f.test();
f.carry(); // calls Fred's m_fn()
return 0;
}
How can I make this code work if I want class Fred/carry() to call handlers
set in CMyFred ?
If typedef int (Fred::*FredMemFn)(char x, float y) is a pointer to a Fred
member function, won't a member function in CMyFred (subclass of Fred) with
same signature be accepted?
Hope my question was clear
Regards,
Elias
#include <iostream>
#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
class Fred;
typedef int (Fred::*FredMemFn)(char x, float y);
class Fred
{
private:
FredMemFn m_fn;
public:
int f(char x, float y);
int g(char x, float y);
int h(char x, float y) { std::cout << "Fred's h()" << std::endl; return
0;}
int i(char x, float y);
void SetFN(FredMemFn f) { m_fn = f; }
void carry()
{
CALL_MEMBER_FN(*this, m_fn)('x', 1);
}
};
class CMyFred : public Fred
{
public:
int x(char a, float b)
{
std::cout << "MyFred's x()" << std::endl;
}
void test();
};
void CMyFred::test()
{
// this works
SetFN(Fred::h);
// this doesn't <-- how to make it work?
SetFN(x);
}
int main()
{
CMyFred f;
f.test();
f.carry(); // calls Fred's m_fn()
return 0;
}
How can I make this code work if I want class Fred/carry() to call handlers
set in CMyFred ?
If typedef int (Fred::*FredMemFn)(char x, float y) is a pointer to a Fred
member function, won't a member function in CMyFred (subclass of Fred) with
same signature be accepted?
Hope my question was clear
Regards,
Elias