Thread: Function pointers

  1. #1

    Function pointers

    I have a C++ class -- CUtility. I made an instance of the class, Utility. All the tutorials on function pointer I've read require you use static functions, but I need to use a non-static function from the instance of my CUtility class. How can I do this?

  2. #2
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    The only difference between a static and nonstatic function is that a static function only has file scope. A nonstatic (external) function can be seen by other files. There's absolutely nothing wrong with using an external function as the address for a function pointer.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    161
    Code:
    
    class CUtility
    {
    public:
      void setNum(int i) { num = i; } 
    private:
      int num;
    };
    
    typedef void (CUtility::*pointerToMember)(int);
    
    int main()
    {
      pointerToMember func = &CUtility::setNum;
      CUtility utility;
    
      (utility.*func)(10);
    
      return 0;
    }

  4. #4
    I did that and got this (MSVC++ 6)

    Code:
    H:\JOSH'S MY DOCUMENTS\Simple 3D Engine\engine.cpp(59) : fatal error C1001: INTERNAL COMPILER ERROR
            (compiler file 'msc1.cpp', line 1786) 
             Please choose the Technical Support command on the Visual C++ 
             Help menu, or open the Technical Support help file for more information

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    161
    Hrm, I just compiled this on both gcc 3.2 and MSVC6. You may need to upgrade to SP5.

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, 09: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, 01: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, 04:22 PM