Function Pointers

This is a discussion on Function Pointers within the C++ Programming forums, part of the General Programming Boards category; Is there a way I could store a class function in a variable and have a particular class instance call ...

  1. #1
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186

    Function Pointers

    Is there a way I could store a class function in a variable and have a particular class instance call the function (in effect, the variable would simply tell the class which of "its own" functions to call):

    Code:
    class SomeClass;
    
    typedef int (SomeClass::*SOMECLASSFUNC)();
    
    class SomeClass {
       public:
          int Initialize();
    };
    
    
    ...
    
    
    SomeClass *classInstance;
    SOMECLASSFUNC scf;
    
    classInstance = new SomeClass;
    scf = &SomeClass::Initialize();
    
    classInstance->scf();   // looking for a piece of code that's equivalent to this in theory
                            // compiler, of course, returns an error because there's no
                            // such SomeClass::scf() function.
    Thanks in advance.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  2. #2
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Got it, just needed a dereference operator and some parentheses:

    Code:
    (classInstance->*scf)();
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

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, 07: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, 02:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 04:22 PM

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