function pointer inner member function of class C++

This is a discussion on function pointer inner member function of class C++ within the C++ Programming forums, part of the General Programming Boards category; Hi, i've got problem with function pointer, the program crash when i try to run, compiler is OK (gcc version ...

  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
    22,303
    Perhaps you meant to write:
    Code:
    int (one::*func)(int, int) = &one::min;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    32,851
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 02:09 PM
  2. Member function pointer within the class
    By skorman00 in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2004, 07: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, 01:41 PM
  5. Function pointer to class member?
    By no-one in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2002, 09:23 PM

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