Thread: Pointers to functions problem

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    25

    Pointers to functions problem

    Solved. Related question: Is there a way to create a function pointer that can point to a member of any class?

    Can someone share with me the correct way to use a function belonging to another class through function pointers? Neither of these seem to work:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class newtest
    {
    	int newtest::addMe(int a, int b)
    	{
    		return a + b;
    	}
    
    	newtest::newtest(int (*functocall)(int,int))
    	{
    		int result = (*functocall)(1,2);
    		cout << result;
    	}
    	
    	int main ()
    	{
    		cout << "First attempt: 1 + 2 = ";
    		newtest thisTest(newtest::addMe);   // Error here
    
    		cout << endl << "Second attempt: 3 + 4 = ";
    		int (*reffunc)(int,int) = newtest::addMe;   // Error here
    		int result = (*reffunc)(3,4);
    		cout << result;
    
    		string dontcare;
    		cin >> dontcare;
    		return 0;
    	}
    };
    I've tried with and without the newtest:: preceeding the function name. This will not compile in VC++ 2010. The compile error is not very helpful. "error C3867: 'newtest::addMe': function call missing argument list; use '&newtest::addMe' to create a pointer to member" for both lines. When I add the &, it just changes the error message to an illegal operator one.

    The mouseover messages are a little more descriptive. The first line gives "no instance of constructor 'newtest::newtest' matches the argument list". The second line "a value of type 'int (newtest::*)(int a, int b)' cannot be used to initialize an entity of type 'int (*)(int,int)'".

    What do you guys think?
    Last edited by blakeo_x; 09-24-2011 at 11:02 PM.

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    I found a solution, courtesy of a set of tutorials for this that I somehow couldnt find before. The Function Pointer Tutorials - Syntax

    First, sorry I put the main() inside the class body. Second, sorry I forgot to declare the members public. Third, for anyone curious to how I solved the issue, the compiler was right about * not being the same as newtest::*. So, I had to change the references to be of the "right type". That's basically all there was to it.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    class newtest
    {
    public:
    	int newtest::addMe(int a, int b)
    	{
    		return a + b;
    	}
    
    	newtest::newtest(int (newtest::*functocall)(int,int))
    	{
    		int result = (*this.*functocall)(1,2);
    		cout << result;
    	}
    };
    
    int main ()
    {
    	cout << "First attempt: 1 + 2 = ";
    	newtest thisTest(&newtest::addMe);
    
    	cout << endl << "Second attempt: 3 + 4 = ";
    	int (newtest::*reffunc)(int,int) = &newtest::addMe;
    	int result = (thisTest.*reffunc)(3,4);
    	cout << result;
    
    	string dontcare;
    	cin >> dontcare;
    	return 0;
    }
    Now my question is, is there a way to create a function pointer that can point to a member of any class?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, to be honest, you also forgot that, unlike normal function pointers, you must use a & on the function to get the address. An oversight in the original standard, yet now fixed for member functions.
    Also, note that when calling a member function, you need to specify the member function of whose instance you want to call.

    Using
    (*this.*functocall)(1,2);
    is pretty pointless.
    You would probably want something like
    (instance.*function_to_call)(1, 2);
    Where instance is a (const) newtest&.

    However, for all of that, there is much simpler way (and more practical) to accept a function pointer. You simply accept a template:
    template<typename T>
    newtest::newtest(T function_to_call)

    Then,
    function_to_call(1, 2);
    will always work so long as what you pass in supports the operator () syntax, may that be a function pointer, member function pointer, lambda or just some custom functor.
    There is also std::function for dynamically binding a function object like described above. It has a slight overhead, though, so these two methods complement each other.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem passing pointers between functions
    By Civus in forum C Programming
    Replies: 7
    Last Post: 09-17-2011, 09:53 PM
  2. Returning pointers from functions problem.
    By Swerve in forum C++ Programming
    Replies: 9
    Last Post: 03-05-2010, 01:35 AM
  3. pointers to functions problem
    By sept in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2007, 04:16 PM
  4. Problem with arrays, pointers and functions when combined
    By The Wazaa in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2006, 10:44 AM
  5. Problem with pointers and functions
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2005, 12:40 PM