function pointers...

This is a discussion on function pointers... within the C++ Programming forums, part of the General Programming Boards category; Hi all, i have a certain certain command code that seems ambigues to me and I'm forced to use it. ...

  1. #1
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    function pointers...

    Hi all, i have a certain certain command code that seems ambigues to me and I'm forced to use it. Can anyone explain the meaning of this particular line of code: ???

    Code:
    typedef void (*Function_Type)(ItemType &item);
    i'm having problem with comprehending the use "typedef" along with the entire function pointer...
    my first inclination was that this is a pointer to a function which returns void and takes (ItemType &item) as an argument...??


    and just for reference, I have a function that takes that same function pointer as an argument and inside calls another function whose (obviously) pointer to was passed in: for example

    Code:
    void traverse(Function_Type visit)
    {
          for(int i = 0; i < size; i++)
          {
              visit(array[i]);
           }
    }

    now, with all this said, i was trying to call the function "traverse()" but i'm always getting compiler errors, even when i try to use different way of expressing parameters i.e:


    Code:
    // function call
    
    traverse(display(ItemType &item));
    something like that, but i can't get it to work,
    can anyone help???

    I hope I made my question clear enough??? if not let me know???

    Thanks....



    matheo917

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    No, it would be:
    traverse(&display);

    Notice, its'a pointer to a function, not a reference to one
    typedef void (*Function_Type)(ItemType &item);

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    thank you for the reply...

    you see, actually i think i'm supposed to use an "address operator" when i call the "traverse" function, i.e:

    Code:
    traverse(&display);
    since a pointer needs an address....

    anyway, traverse(), as well as, display() are member functions of the class and i can't seem to get the proper notation for (display) b/c it says display is undeclared undentifier

    so traverse actually gets called using an object of that class
    i.e:

    Code:
    T.traverse(&display);
    but that's when i get an error of an "undeclared undentifier" - display

    and

    Code:
    T.traverse(&T.display);
    doesn't work either....

    i'm trying to find the solution without violating the rules of an ABSTRACT DATA TYPES such as tables or stacks or queues etc. and not giving access to undesired areas...

    or perhaps i need to make something global here or another typedef somewhere???


    any ideas????

    thanks

    .........................
    matheo917

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    standard c style function pointer syntax doesnt work for member functions. You have to use pointer to member operators. Look up operator ->* and operator .* in your helpfiles.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    No, it would be:
    traverse(&display);
    It could be either one. Since the only two things you can do with a function are call it and take its address, if you aren't calling it then C++ assumes you're taking its address. So the & to return its address isn't required.
    anyway, traverse(), as well as, display() are member functions of the class and i can't seem to get the proper notation for (display) b/c it says display is undeclared undentifier
    Pointers to members have different syntax
    Code:
    #include <iostream>
    
    using namespace std;
    
    class T {
    public:
        void speak(int);
    };
    
    void T::speak(int num)
    {
        cout<< num <<endl;
    }
    
    typedef void (T::*FunctionPtr)(int);
    
    int main()
    {
        T t;
        FunctionPtr p = &T::speak;
    
        for (int i = 0; i < 5; i++)
            (t.*p)(i);
    }

  6. #6
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    i see................

    but what about if (in your example) speak() function needs to take a Function_Type (pointer to a function) as a parameter???

    in other words, what i mean is: since (in my example) the function traverse takes a pointer to another function which is in the public domain of the class it looks like such:

    Code:
    T {
    public:
     
            void traverse(T::*Function_Type visit);
    
    private:
    
    };
    then essentially i would have to put the "typedef" before the declaration of the class so the traverse() function knows what Function_Type is, but on the other hand if I do, in fact, put it before the declaration of the class then the "typedef" expression doesn't know what " T " is........hmmmm

    i'm getting myself lost here...........

    and of course it would be easier if, like in your example, the speak() function would take in some integers but in my case it has to receive a pointer to another function of the class as an argument...

    thanks for the efforts


    matheo917
    ...............................................

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    just use a forward declaration before the typedef. something like

    class T;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    but what about if (in your example) speak() function needs to take a Function_Type (pointer to a function) as a parameter???
    Forward references, this pointers, pointer to member functions, member functions that take member functions of the same class as arguments... And people say that C++ is too complicated, ha!
    Code:
    #include <iostream>
    
    using namespace std;
    
    class T;
    typedef void (T::*FunctionPtr)();
    
    class T {
    public:
        void speak(FunctionPtr);
        void blah();
    };
    
    void T::speak(FunctionPtr fp)
    {
        (this->*fp)();
    }
    
    void T::blah()
    {
        cout<<"Blah"<<endl;
    }
    
    int main()
    {
        T t;
        FunctionPtr p = &T::blah;
    
        t.speak(p);
    }

  9. #9
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    nice.........

    thanks for help it seems to work properly now...

    interesting how the "this" pointer gets used here,

    i must read up on the uses of (->*) operator and study a bit your line of code:

    (this->*fp)();

    interestingly by simply using "visit()" the compiler couldn't evalute that to a function call....


    thanks....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  2. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 03:22 PM

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