Thread: Passing a function to a function

  1. #1
    lend0g
    Guest

    Passing a function to a function

    Can anybody give me an example of passing a function to a function. i have to do one for a current project and i need some help on it. here is the class member function

    void Tran(void Lookup(Name,Value));

    the function LookUp is declared at the top of Main().
    If u know how, u dont need to use what i have above, just please give me an example on how i would call a class member function in main with a parameter like this, if u can please help. thank you

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    you would probably wanna make

    this:

    void Tran(void Lookup(Name,Value));

    this: using a pointer

    void Tran(void (*Lookup)(type Name,type Value));

    for member functions its done like so

    void Tran(void (myclass::*myfunc) (type Name, type Value));

    or whatever.

    edit::

    actually i think an exampe is in order, since you'll most likely need one on how to call a non-static method pointer.

    the simplest explaination is that a functions pointer to a non static class method requires an instance of the class inorder to be called.

    ie:

    Code:
    #include <iostream>
    using namespace std;
    
    class testClass;
    
    typedef void (testClass::*methodptr)(int i);
    
    class testClass
    {
    public:
    	void method(int i);
    	void method2(methodptr met);
    };
    
    void testClass::method(int i)
    { 
    	cout << i << endl;
    }
    
    void testClass::method2(methodptr met)
    {
    	(this->*met)(10);
    	(*this.*met)(1000);
    }
    
    int main(void)
    {	
    	testClass tci;
    	testClass* tcp = &tci;
    	
    	methodptr meth = tci.method;
    	tci.method2(tci.method);
    	(tci.*meth)(1);
    	(tcp->*meth)(100);
    	
    	return 0;
    }
    Last edited by no-one; 03-19-2003 at 03:16 AM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Passing A Function Into A Constructor
    By fourplay in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2009, 06:06 AM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM