Thread: function pointer inner member function of class C++

  1. #1
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23

    function pointer inner member function of class C++

    Hi, i've got problem with function pointer, the program crash when i try to run, compiler is OK (gcc version 4.3.2). thanks a lot for any helping.

    Code:
    #include <iostream>
    class one {
    public:
        int min(int a, int b) {
            if (a < b) return a;
            else return b;
        }
        int guru() {
            int (one::*func)(int, int) = NULL;
            return (*this.*func)(2,3);
         }
    };
    int main() {
        one o;
        o.guru(); // crash
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you meant to write:
    Code:
    int (one::*func)(int, int) = &one::min;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You seem to be pointing at NULL, not min.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23
    It's work, thanks very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-23-2006, 01:09 PM
  2. Member function pointer within the class
    By skorman00 in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2004, 06:03 AM
  3. syntax to pass a member function pointer to another class?
    By reanimated in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2003, 05:24 PM
  4. derived class member function pointer
    By btq in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2002, 12:41 PM
  5. Function pointer to class member?
    By no-one in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2002, 08:23 PM