GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Dec-2005, 06:25
svenveer svenveer is offline
New Member
 
Join Date: Oct 2005
Posts: 15
svenveer is on a distinguished road

passing a function pointer in to a function (c++)


Can I Pass a function pointer to a function?

something like:

CPP / C++ / C Code:
class Bar;
public foo(Bar*);

class FooBar{
     void (*fp)(Bar*);
     <snip/>
         fp = foo;
     public:
         (*fp)(Bar*) get_fp(){
             return fp;
         }
}

class Foo{
     void (*fp)(Bar*);
     <snip/>
         fp = foobar->get_fp();
}
Last edited by LuciWiz : 03-Dec-2005 at 07:40. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 02-Dec-2005, 07:17
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 922
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Arrow

Passing and returning a function pointer with a function using C++


Sven,

We can pass function pointer to a function.
We can return function pointer from a function. // This is what your code tells.

You have to modify your code a little bit.
Here is a simple example using classes, that explains passing a function pointer and also returning a function pointer...
CPP / C++ / C Code:
#include<iostream>
using namespace std;

class Bar                   // a sample class Bar
{
    public:             
        int i;              // assume a simple variable for testing.
        Bar()
        {
            i = 10;         // initialize i to 10 in constructor
        }
};


void foo(Bar* x)        // function that takes Bar* as argument.
{
    cout<<"The value of i is "<<x->i<<endl;         // print the value of i in the Bar Object
}

//IMPORTANT PART

typedef void (*fp)(Bar*);   // using typedef simplifies coding

// It also aids in easy return of function pointer


class FooBar
{          
    fp ptr;                 // create new function pointer to foo
   
public: 
    FooBar()                // initialize function pointer to foo in constructor
    {
        ptr = &foo;
    }
    fp get_fp()             // a function that returns a function pointer to foo.
    {
        cout<<"Returned a function pointer..."<<endl;
        return ptr;
    }
    void put_fp(fp x)       // a function that has function as the argument.
    {
        ptr = x;
        cout<<"Changed the Function pointer..."<<endl;
    }
};


int main()
{
    FooBar *foobar = new FooBar();  // create new Object

    fp ptr;                         // a function pointer to foo

    ptr = foobar->get_fp();         // THERE!!!! We use the function to initialize function pointer

    Bar x;                          // create a new Bar object to verify
    (*ptr)(&x);                     // See the output!!!!!

    foobar->put_fp(ptr);            // passing function pointer to a function

    return 0;
}
Using typedef, we make the code simpler and coding easier.

And sven, dont forget to put semicolon in the end of the class declaration.

Best Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
[Tutorial] Function Pointers aaroncohn CPP / C++ Forum 4 17-Feb-2006 11:33
Pointer Usage in C++: Beginner to Advanced varunhome CPP / C++ Forum 0 19-Aug-2005 09:25
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:50.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.