Problem with function pointers

This is a discussion on Problem with function pointers within the C++ Programming forums, part of the General Programming Boards category; I have this setup: I have a class, and one of the members is a function pointer. I want to ...

  1. #1
    Banned
    Join Date
    May 2004
    Posts
    129

    Problem with function pointers

    I have this setup:

    I have a class, and one of the members is a function pointer. I want to be able to implement various possible versions of the function it points to in the same class the function pointer resides in. However, I get various errors when I try to initialize the function pointer to a function that also is a member of the same class.

    I've shown the first part, then the error it generates, then in the second part I try to remedy it by declaring the function pointer to be a member of the Vec class, but it still doesn't work (I show the error codes of visual C++ 6). Of course, I couldn't then put this same function pointer in the baseclass, but it could at least point to a member function (sadly I can't even get this much to work properly).

    Code:
    #include	<iostream>
    using namespace std;
    
    //pFunc is of data type function pointer which returns void, accepts void as formal parameter
    typedef	void(*pFunc)(void);
    
    void	SomeFunc()
    {
    	cout	<<	"This is just some func" << endl;
    }
    
    typedef	struct	
    {
    	Vec()
    	{
    		x = y = z = w = 0;
    		funcPtr	=	0;
    	}
    	void	VectorFunc()
    	{
    		cout	<<	"This is the vector func" << endl;
    	}
    	pFunc	funcPtr;
    	float	x;
    	float	y;
    	float	z;
    	float	w;	//complex plane
    }Vec,*VectorPTR;
    
    typedef	 void(Vec::*VecFuncPtr)(void);
    
    int	main(void)
    {
    	Vec a;
    	a.funcPtr	=	SomeFunc; //works
    	a.funcPtr();
    	
    	a.funcPtr	=	Vec::VectorFunc;	//error:
    	/*
    	error C2440: '=' : cannot convert from 'void (__thiscall Vec::*)(void)' to 'void (__cdecl *)(void)'
    	*/
    
    	VecFuncPtr b;
    	b = Vec::VectorFunc;
    	b();	//error:
    	/*
    		error C2064: term does not evaluate to a function
    	*/
    
    	return 0;
    }
    Last edited by vNvNation; 06-12-2004 at 04:49 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,681
    There's a big difference between regular function pointers and member function pointers. There's also a little extra syntax that's required for declaring and invoking a pointer to member function.

    Hopefully this site will fill in the gaps for ya:
    http://www.function-pointer.org/

    gg

  3. #3
    Banned
    Join Date
    May 2004
    Posts
    129
    Hi, that is a very good site, and subsequently I am in the process of answering my questions.

    I appreciate the help, I shall throw some reputation your way

    Have a nice day.

    edit

    I take that back, it is an EXCELLENT site. I am rather enjoying myself reading the site and going through the examples (I'm going through with functors right now).
    Last edited by vNvNation; 06-12-2004 at 06:55 PM.

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    > float (*GetPtr1(const char opCode)) (float, float);
    how can this return a pointer to a function, this prototpye is a little messy?
    The return type surrounds the function declaration. So, the function GetPtr1 returns a pointer to a function that takes two floats as parameters and returns a float. GetPtr1 itself takes a single const char as a parameter. I would recommend typedefing your function pointers first, it looks a lot neater.

    >typedef void(Vec::*VecFuncPtr)(void);
    i know how typedef works, but what synonim does this typedef creates and to what?
    It is a pointer to a member function of class Vec that takes no parameters and has no return value.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,681
    Quote Originally Posted by http://www.function-pointer.org
    2.7 How to Return a Function Pointer ?
    It's a little bit tricky but a function pointer can be a function's return value. In the following example there are two solutions of how to return a pointer to a function which is taking two float arguments and returns a float. If you want to return a pointer to a member function you have just got to change the definitions/declarations of all function pointers.
    Code:
     //-----------------------------------------------------------------------------------------
       // 2.7 How to Return a Function Pointer
       //     'Plus' and 'Minus' are defined above in 2.1-4. They return a float and take two float
    
    
       // direct solution
       // function takes a char and returns a pointer to a function which is taking two
       // floats and returns a float. <opCode> specifies which function to return
       // Yes it's messy, that's why you should use a typedef
       float (*GetPtr1(const char opCode))(float, float)
       {
          if(opCode == '+')  return &Plus;
          if(opCode == '-')  return &Minus;
       }
    
    
       // solution using a typedef
       // define a pointer to a function which is taking two floats and returns a float
       // "pt2Func" is now a synonym type name for a pointer to function with that signature
       typedef float(*pt2Func)(float, float);
    
       // function takes a char and returns a function pointer which is defined as a
       // type above. <opCode> specifies which function to return
        // as you can see, "pt2Func" is the return type and is alot easier to read
       pt2Func GetPtr2(const char opCode)
       {
          if(opCode == '+')  return &Plus;
          if(opCode == '-')  return &Minus;
       }
    
    
       // execute example code
       void Return_A_Function_Pointer()
       {
          cout << endl << "Executing 'Return_A_Function_Pointer'" << endl;
    
          float (*pt2Function)(float, float);     // define a function pointer
    
          pt2Function=GetPtr1('+');               // get function pointer from function 'GetPtr1'
          cout << pt2Function(2, 4) << endl;      // call function using the pointer
    
    
          pt2Function=GetPtr2('-');               // get function pointer from function 'GetPtr2'
          cout << pt2Function(2, 4) << endl;      // call function using the pointer
       }
    gggg

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, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Class function pointers
    By VirtualAce in forum C++ Programming
    Replies: 40
    Last Post: 02-16-2005, 11:55 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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