Passing a function to a function

This is a discussion on Passing a function to a function within the C++ Programming forums, part of the General Programming Boards category; Can anybody give me an example of passing a function to a function. i have to do one for a ...

  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,267
    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, 09:28 AM
  2. Passing A Function Into A Constructor
    By fourplay in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2009, 07: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, 04:33 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM

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