CMapStringToPtr with ptrs to member functions

My problem can be illustrated clearly with this example.

Let us say I have this class definition:

class TEST
{ public:

  TEST ();
  ~TEST ();
  CMapStringToPtr m_FunctionMap;
  void Function (void);
};

TEST::TEST ()
{ m_FunctionMap.SetAt ("Testing", Function); }

TEST::~TEST ()
{
}

void TEST::Function (void)
{ return; }

How can I modify the:
m_FunctionMap.SetAt ("Testing", Function);

so that given the "Testing" string, the CMap will give a pointer to this member function?

Thank you
IgorStravinskyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gus012498Commented:
I guess it wont work because Function is not a static member.

Regards Gus
0
Oliver_DornaufCommented:
Pointer to non static memberfunctions is tricky !

class test {
public:
 typedef void (test::*pmf)();
  //signature of the memberfunctions
  test() {
    m_FuntionMap.SetAt (CString("fkt1"), &test::fkt1);
    m_FuntionMap.SetAt (CString("fkt2"), &test::fkt2);
  ~test() {;}

    void fkt1();
      // must same signature as pmf
    void fkt2();

    CMap<LPCSTR,LPCSTR, pmf, pmf> m_FuntionMap;
};

..
..
..
test* myTest = new test();
CString string("fkt1");

test::pmf funtionPtr;
myTest->m_FuntionMap.Lookup (string, funtionPtr);

(myTest->*funtionPtr)();
..
..
..




}




   




 // typedef int (test::*pmf)(int, bool)







 
0
IgorStravinskyAuthor Commented:
Hello,

I am having a problem with your code.  Here is my implementation (the complete code):

--------------------------------

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "test.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

class test
{ public:
  typedef void (test::*pmf) ();
  //signature of the memberfunctions

  test ()
  { m_FunctionMap.SetAt (CString ("fkt1"), &test::fkt1);
    m_FunctionMap.SetAt (CString ("fkt2"), &test::fkt2);
  }

  ~test() {; }

  void fkt1 ();
  void fkt2 ();

  CMap<LPCSTR, LPCSTR, pmf, pmf> m_FunctionMap;
};

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
  int nRetCode = 0;

  // initialize MFC and print and error on failure
  if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  {
    // TODO: change error code to suit your needs
    cerr << _T("Fatal Error: MFC initialization failed") << endl;
    nRetCode = 1;
  }
  else
  {
    // TODO: code your application's behavior here.
    CString strHello;
    strHello.LoadString(IDS_HELLO);
    cout << (LPCTSTR)strHello << endl;
  }

  test* myTest = new test();
  CString string("fkt1");

  test::pmf functionPtr;
  myTest->m_FunctionMap.Lookup (string, functionPtr);

  (myTest->*functionPtr)();

  return nRetCode;
}

--------------------------------

I get these compiler errors

F:\test\test.cpp(28) : error C2143: syntax error : missing ';' before '<'
F:\test\test.cpp(28) : error C2501: 'CMap' : missing storage-class or type specifiers
F:\test\test.cpp(28) : error C2059: syntax error : '<'
F:\test\test.cpp(28) : error C2238: unexpected token(s) preceding ';'
F:\test\test.cpp(61) : error C2039: 'm_FunctionMap' : is not a member of 'test'
        F:\test\test.cpp(14) : see declaration of 'test'
F:\test\test.cpp(61) : error C2228: left of '.Lookup' must have class/struct/union type

Line 28 is:
CMap<LPCSTR, LPCSTR, pmf, pmf> m_FunctionMap;

What is wrong with this statement?  What should I do to fix this problem?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

IgorStravinskyAuthor Commented:
I am going to reject your answer until you or someone else can fix the problem.  Thanks.
0
Oliver_DornaufCommented:
#include <afxtempl.h> to get the definition of CMap.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IgorStravinskyAuthor Commented:
It is always the simple things that cause the problems. =o)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.