Thread: Function pointers in C++ classes

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    2

    Function pointers in C++ classes

    Hi.

    I'm trying to pass a pointer to a function in a class as a parameter to another class' constructor, but I'm not sure how to do this.
    I created a small example-program:
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    class A
    {
    public:
        A(int n) { this->n = n; }
        ~A() {}
    
        int funcA() { return n; };
    private:
        int n;
    };
    
    class B
    {
    public:
        B(int (*func)()) { cout << func() << endl;}
        ~B() {}
    };
    
    int main()
    {
        A* a = new A(1338);
        B* b = new B(a->funcA);
    
        return 0;
    }
    The output from g++:
    test.cpp: In function `int main()':
    test.cpp:27: error: no matching function for call to `B::B(<unknown type>)'
    test.cpp:18: error: candidates are: B::B(const B&)
    test.cpp:20: error: B::B(int (*)())

    What I want it to do is to print "1338" when it's run.
    What am I doing wrong?

    Thanks in advance.

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    http://www.function-pointer.org/fpt.html#defifunction-pointer.org is a good place to learn about function pointers, I think. Although, I haven't had any luck with function pointers to member functions myself... Maybe you can get it to work though, after all it's part of the language
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Its easy. Just place the name of the class and the scope resolution operators before the name of the function:
    Code:
    void (MyClass::*func)(int);
    To call this function, you need to send it an object. To send the current object in:
    Code:
    (this->*func)(8);
    The basic syntax of a call is:
    Code:
    (object.*ptr)(args);

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Quote Originally Posted by Speedy5
    Its easy. Just place the name of the class and the scope resolution operators before the name of the function:
    Code:
    void (MyClass::*func)(int);
    You can do this, but you will also have to declare the function as static in order to do that. That would fix your problem in the first place, as the link that Hunter2 gave you will point out. I was given that link by codeplug a few weeks back, and it helped me out tremendously.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    No you don't, the function does not have to be static.

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    if you're trying to pass a class member function, then that function needs to be declared as static. The reason being is that a member function has a different calling convention than a stand-alone function. Making it static will give it that calling convention you need.

    Why you ask? Because a member function also passes this as an argument without even telling you; they're sneaky.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's why there's the (MyClas::*) thing in the pointer declaration. It marks it as a member function. That's also why you do object.*ptr() to call the thing, because it then passes object as the this pointer.

    You need static methods for pointers to global functions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Because this object is not implied, you must provide it and thats why you have the object.*ptr() thing. That object acts like the this pointer you're talking about.

    The member functions do not have to be static for you to get a pointer to them.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here's a quick synopsis (in addition to the info at function-pointer.org)

    Expressions with Pointer-to-Member Operators

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. 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