Pointer to member function in struct
-
Hi, Since I knew The Code Project, it has been helpful a lot. Now I think I have a small misunderstanding about pointer to member function in a struct and I need your help, which will be priceless to me. I am simply trying to see how pointer to member function works and it seems a little tricky. The code I am having a problem with is below.
#include #include #define KEY_ESC 27
using std::cout;
using std::endl;class CAccount;
struct StateMap
{
int input;
void (CAccount::*mfp)();
};class CAccount
{
public:
enum
{
STATE_IDLE,
STATE_INPUT,
STATE_WAIT_ACK
};public:
StateMap map[3];
int m_iState;CAccount(); void OnIdle(); void OnInput(); void OnWaitAck();
};//class CAccount
CAccount::CAccount(){
map[0].input = 1;
map[0].mfp = &CAccount::OnIdle;map\[1\].input = 2; map\[1\].mfp = &CAccount::OnInput; map\[2\].input = 3; map\[2\].mfp = &CAccount::OnWaitAck; m\_iState = STATE\_IDLE;
}
void CAccount::OnIdle()
{
cout << "OnIdle()" << endl;
}void CAccount::OnInput()
{
cout << "OnInput()" << endl;
}void CAccount::OnWaitAck()
{
cout << "OnWaitAck()" << endl;
}int main()
{
int ch = 0;
int i;
CAccount account;while ( ch != KEY\_ESC ) { ch = \_getch(); i = 0; while ( account.map\[i\].input != 0 ) { if ( ch == account.map\[i\].input ) { (account.(map\[i\].\*mfp))(); // compile error }//if i++; }//while }//while return 0;
}//main()
I just don't know the right syntax for that part. How should I change that error line to make it work? Thanks in advance.
-
Hi, Since I knew The Code Project, it has been helpful a lot. Now I think I have a small misunderstanding about pointer to member function in a struct and I need your help, which will be priceless to me. I am simply trying to see how pointer to member function works and it seems a little tricky. The code I am having a problem with is below.
#include #include #define KEY_ESC 27
using std::cout;
using std::endl;class CAccount;
struct StateMap
{
int input;
void (CAccount::*mfp)();
};class CAccount
{
public:
enum
{
STATE_IDLE,
STATE_INPUT,
STATE_WAIT_ACK
};public:
StateMap map[3];
int m_iState;CAccount(); void OnIdle(); void OnInput(); void OnWaitAck();
};//class CAccount
CAccount::CAccount(){
map[0].input = 1;
map[0].mfp = &CAccount::OnIdle;map\[1\].input = 2; map\[1\].mfp = &CAccount::OnInput; map\[2\].input = 3; map\[2\].mfp = &CAccount::OnWaitAck; m\_iState = STATE\_IDLE;
}
void CAccount::OnIdle()
{
cout << "OnIdle()" << endl;
}void CAccount::OnInput()
{
cout << "OnInput()" << endl;
}void CAccount::OnWaitAck()
{
cout << "OnWaitAck()" << endl;
}int main()
{
int ch = 0;
int i;
CAccount account;while ( ch != KEY\_ESC ) { ch = \_getch(); i = 0; while ( account.map\[i\].input != 0 ) { if ( ch == account.map\[i\].input ) { (account.(map\[i\].\*mfp))(); // compile error }//if i++; }//while }//while return 0;
}//main()
I just don't know the right syntax for that part. How should I change that error line to make it work? Thanks in advance.
Try the following :) :
//...
typedef void (*LPSTATEPROC) (void);struct StateMap
{
int input;
LPSTATEPROC mfp;
};
//...They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)
-
Try the following :) :
//...
typedef void (*LPSTATEPROC) (void);struct StateMap
{
int input;
LPSTATEPROC mfp;
};
//...They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)
Thanks.
Eugen Podsypalnikov wrote:
typedef void (*LPSTATEPROC) (void);
You meant this, right?
typedef void (CAccount::*LPSTATEPROC) (void);
But it seems like it doesn't still work and the error occurs at the same line.
#include
#include#define KEY_ESC 27
using std::cout;
using std::endl;class CAccount;
typedef void (CAccount::*LPSTATEPROC) (void);
struct StateMap
{
int input;
LPSTATEPROC mfp;
};
class CAccount
{
public:
enum
{
STATE_IDLE,
STATE_INPUT,
STATE_WAIT_ACK
};public:
StateMap map[3];
int m_iState;CAccount(); void OnIdle(); void OnInput(); void OnWaitAck();
};//class CAccount
CAccount::CAccount(){
map[0].input = 1;
map[0].mfp = &CAccount::OnIdle;map\[1\].input = 2; map\[1\].mfp = &CAccount::OnInput; map\[2\].input = 3; map\[2\].mfp = &CAccount::OnWaitAck; m\_iState = STATE\_IDLE;
}
void CAccount::OnIdle()
{
cout << "OnIdle()" << endl;
}void CAccount::OnInput()
{
cout << "OnInput()" << endl;
}void CAccount::OnWaitAck()
{
cout << "OnWaitAck()" << endl;
}int main()
{
int ch = 0;
int i;
CAccount account;while ( ch != KEY\_ESC ) { ch = \_getch(); i = 0; while ( account.map\[i\].input != 0 ) { if ( ch == account.map\[i\].input ) { (account.(map\[i\].\*mfp))(); // compile error }//if i++; }//while }//while return 0;
}//main()
How can I change that error line to make it work? Thanks!
-
Try the following :) :
//...
typedef void (*LPSTATEPROC) (void);struct StateMap
{
int input;
LPSTATEPROC mfp;
};
//...They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)
-
Thanks.
Eugen Podsypalnikov wrote:
typedef void (*LPSTATEPROC) (void);
You meant this, right?
typedef void (CAccount::*LPSTATEPROC) (void);
But it seems like it doesn't still work and the error occurs at the same line.
#include
#include#define KEY_ESC 27
using std::cout;
using std::endl;class CAccount;
typedef void (CAccount::*LPSTATEPROC) (void);
struct StateMap
{
int input;
LPSTATEPROC mfp;
};
class CAccount
{
public:
enum
{
STATE_IDLE,
STATE_INPUT,
STATE_WAIT_ACK
};public:
StateMap map[3];
int m_iState;CAccount(); void OnIdle(); void OnInput(); void OnWaitAck();
};//class CAccount
CAccount::CAccount(){
map[0].input = 1;
map[0].mfp = &CAccount::OnIdle;map\[1\].input = 2; map\[1\].mfp = &CAccount::OnInput; map\[2\].input = 3; map\[2\].mfp = &CAccount::OnWaitAck; m\_iState = STATE\_IDLE;
}
void CAccount::OnIdle()
{
cout << "OnIdle()" << endl;
}void CAccount::OnInput()
{
cout << "OnInput()" << endl;
}void CAccount::OnWaitAck()
{
cout << "OnWaitAck()" << endl;
}int main()
{
int ch = 0;
int i;
CAccount account;while ( ch != KEY\_ESC ) { ch = \_getch(); i = 0; while ( account.map\[i\].input != 0 ) { if ( ch == account.map\[i\].input ) { (account.(map\[i\].\*mfp))(); // compile error }//if i++; }//while }//while return 0;
}//main()
How can I change that error line to make it work? Thanks!
I think the line:
(account.(map\[i\].\*mfp))(); // compile error
should not use indirection, and should read:
(account.(map\[i\].mfp))();
Unrequited desire is character building. OriginalGriff
-
I think the line:
(account.(map\[i\].\*mfp))(); // compile error
should not use indirection, and should read:
(account.(map\[i\].mfp))();
Unrequited desire is character building. OriginalGriff
it is error too!
-
it is error too!
What error?
Unrequited desire is character building. OriginalGriff