Pointer to a Member Function

This is a discussion on Pointer to a Member Function within the C++ Programming forums, part of the General Programming Boards category; I've need to assign a pointer to a function, which is easy enough.. but the function in question is a ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    30

    Pointer to a Member Function

    I've need to assign a pointer to a function, which is easy enough.. but the function in question is a public member of a class. Visual Studio is giving me this error:

    Code:
    error C2440: '=' : cannot convert from 'void (__thiscall CTextManager::* )(int)' to 'void (__cdecl *)(int)'
    How can I make a pointer to a member function? Or is this impossible/hackish?

    [EDIT] Forgot to mention that the member function is non-static
    Last edited by TheDan; 03-30-2006 at 12:41 PM.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    post some code also

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    30
    I wasn't sure how much it would help... But heres the function in question:

    Code:
    void CTextManager::Open(SOpenParams *pParams)
    {
    	m_szText = "";
    	pParams->Update = UpdateText;
    
    	m_pFileManager->Open(pParams);
    }
    Code:
    class CTextManager
    {
    public:
    	CTextManager(HWND hWnd);
    	~CTextManager();
    
    	// Text Manager Functions
    	void SetFileManager(CFileManager *pFileManager) { m_pFileManager = pFileManager; }
    	void Resize(RECT rect);
    	void Updated();
    
    	// File Manager Functions
    	void Open(SOpenParams *pParams);
    	void Save(SSaveParams *pParams, bool bSaveAs);
    	void UpdateText(int iProgress);
    
    	// Search Functions
    	void Search();
    private:
    	HMODULE m_hRichEdit;			// Rich edit library
    	HWND m_hParent;					// Main window
    	HWND m_hEdit;					// Edit Control
    	CFileManager *m_pFileManager;	// File Manager
    	CSearch m_Search;				// Search Algorithm
    
    	SSaveParams *m_pSaveParams;
    	SOpenParams *m_pOpenParams;
    	string m_szText;
    
    	bool m_bUpdated;	// Flag to show whether the text has been updated since we last did something
    };
    And the SOpenParams struct

    Code:
    struct SOpenParams
    {
    	LPSTR szText;									// Buffer to store text
    	void (*Opened)(bool bSuccess, SError error);	// Function to call when file has been opened
    	void (*Update)(int progress);					// Function to call to update text
    	bool bUpdated;									// True when Update() has finished
    	int iChunkSize;									// Amount of chars to read in a chunk
    
    	SOpenParams();
    	~SOpenParams();
    
    	CRITICAL_SECTION *pcsFileMgrData;
    private:
    	char const *szFilePath;			// Pointer to the buffer containing the file path
    
    	friend class CFileManager;
    	friend void __cdecl OpenThread(void *pParams);
    };

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    typedef RETURN_TYPE(CLASS_NAME::*NAME)(ARGUMENT_LIST_TYPES);
    ex:
    Code:
    class CStuff
    {
      public:
        void Method(int Arg);
    };
    
    typedef void(CStuff::*Pointer)(int);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I think you declare the function pointer like this:
    Code:
    void (*classname::funcpointer)(args) = classname::somefunc;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    30
    Sorry if I sound stupid, but where would I put that piece of code, dwks? I've not really handled many function pointers before :s certainly not from member functions heh.. =/

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    #include <iostream>
    using namespace std;
    
    class Apple
    {
    public:
    
    	double multiply(int num1, double num2)
    	{
    		return num1 * num2;
    	}
    };
    
    
    int main() 
    {
    	double (Apple::* myFuncPtr)(int, double);
    	myFuncPtr = Apple::multiply;
    
    	Apple myApple;
    	double answer = (myApple.*myFuncPtr)(10, 2.33);
    	
    	cout<<answer<<endl;
    
        return 0;
    }
    Last edited by 7stud; 03-30-2006 at 03:25 PM.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by dwks
    I think you declare the function pointer like this:
    Except the * is in the wrong place!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    30
    Ah thanks 7stud, that clears it up, however a new problem arises... The file in which SOpenParams is in is included by the file which has the class CTextManager in it since CTextManager uses the SOpenParams struct as a member variable.. A forward declare won't work. This is tricky... This is the file linkage of my program

    Code:
    			          Error.h
    				 /       \
    		                /         \
            CSearch.h       CFileManager.h   Error.cpp
           /         \     /              \
          /           \   /                \
         /             \ /                  \
    CSeach.cpp    CTextManager.h	 CFileManager.cpp
    	     /		  \
                /              \
               /                \
           main.cpp         CTextManager.cpp
    SOpenParams is in CFileManager.h
    CTextManager is in CTextManager.h
    SOpenParams is used in the CFileManager files and CTextManager files.

    This means that I cant simply include CTextManager into CFileManager :s
    Last edited by TheDan; 03-30-2006 at 03:39 PM.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    however a new problem arises.
    ...and what is it?

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    30
    I cant declare the pointer as

    void (*CTextManager::UpdateText)(int)

    because CTextManager isnt defined yet, and a forward declare doesnt work because I'm trying to access a member function.

  12. #12
    Registered User
    Join Date
    Apr 2005
    Posts
    30
    Nevermind, I've altered my program design so that this is no longer a problem.

  13. #13
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by Magos
    ex:
    Code:
    class CStuff
    {
      public:
        void Method(int Arg);
    };
    
    typedef void(CStuff::*Pointer)(int);
    Lemme complete this for you (this is how you would use it):

    Code:
    CStuff stuff;
    
    Pointer myFuncPtr = CStuff::Method;
    
    stuff.myFuncPtr(5);

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by homeyg
    Originally Posted by Magos
    Code:
    class CStuff
    {
      public:
        void Method(int Arg);
    };
    
    typedef void(CStuff::*Pointer)(int);
    Lemme complete this for you (this is how you would use it):

    Code:
    CStuff stuff;
    
    Pointer myFuncPtr = CStuff::Method;
    
    stuff.myFuncPtr(5);
    A couple of problems:

    1) There is no function definition anywhere.

    2) VC++6 gives me an error using this syntax:

    stuff.myFuncPtr(5);

    It complains that myFuncPtr is not a member of CStuff. Does that work for you? What compiler are you using?

  15. #15
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by 7stud
    A couple of problems:

    1) There is no function definition anywhere.

    2) VC++6 gives me an error using this syntax:

    stuff.myFuncPtr(5);

    It complains that myFuncPtr is not a member of CStuff. Does that work for you? What compiler are you using?
    Okay.
    Last edited by homeyg; 04-01-2006 at 08:02 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21