static array of function pointers within class

This is a discussion on static array of function pointers within class within the C++ Programming forums, part of the General Programming Boards category; Hello. I'm trying to create a class containing a static data member which is an array of function pointers, but ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    10

    static array of function pointers within class

    Hello. I'm trying to create a class containing a static data member which is an array of function pointers, but I can't work out the syntax to define and initialise it in the corresponding source file. I don't have a problem with defining a global array of function pointers however.


    Code:
    header.hpp
    
    class C
    {
    public:
    	void funcA();
    	void funcB();
    	void funcC();
    
    private:
    	static void (C::*pfunc1[])();
    };
    
    extern void (C::*pfunc2[])();
    
    
    
    
    source.cpp
    
    void C::(C::*pfunc1[])() = { C::funcA, C::funcB, C::funcC }; // gives error 'Identifier expected at line x' on Borland compiler
    
    void (C::*pfunc2[])() = { C::funcA, C::funcB, C::funcC }; // works OK

    Is this too obscure an idea for my own good? Any help appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    Static variables exist before any objects are created. So, a static pointer can't point to a function that only exists as part of an object.
    Last edited by 7stud; 11-01-2005 at 01:07 AM.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Hello 7stud.

    Whilst it is true that (class) static variables are created prior to main() being called, surely this is also the case for globals too, which in the example above compiles OK. Also, member functions do not exist as part of an object in the same way that data members do; they are created at an individual area in memory during compilation as per non-member functions (I think). The member function address is bound to an object whenever that object calls the function, but you do not have to create any objects for the member functions to exist in memory. Therefore there is no problem, as far as I can tell, in assigning a member function address to a class static function pointer; it is the syntax for this operation that I can't work out.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The member function address is bound to an object whenever that object calls the function, but you do not have to create any objects for the member functions to exist in memory.
    What would be the difference between static member functions and regular member functions?

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    As far as function pointers are concerned, non-static member functions are accessed via the pointer-to-member operators, whereas a static member function is accessed via a regular pointer. What I was getting at is that if you define a class and define member functions for that class, it is legitimate to initialise a pointer to a member function (non-static or static) despite having created no objects, as below:

    Code:
    class.hpp
    
    class Feeble
    {
    public:
        void doNowt();
        static void doSFA();
    };
    
    
    class.cpp
    
    #include "class.hpp"
    void Feeble::doNowt(){}
    void Feeble::doSFA(){}
    
    
    main.cpp
    
    #include "class.hpp"
    int main()
    {
        // access non-static member function with pointer-to-member
        void (Feeble::*pf1)() = Feeble::doNowt;
        
        // access static member function through regular pointer
        void (*pf2)() = Feeble::doSFA;
        return 0;
    }
    Not the most useful program ever written, but there you go.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Thread title: static array of function pointers within class.

    Here's an array of function pointers:
    Code:
    void (*functions[10])(int);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Not the most useful program ever written, but there you go.
    Wow! Interesting. Thanks for the enlightening.

    Hold everything. I made a mistake when I was testing your code: I used the pointer to execute the static function, which of course should work without having to create any objects. I don't get any compile errors with your code, but if I add a line in main() to execute the non-static function:

    pf1(); //error C2064: term does not evaluate to a function

    I need to create an object before I can execute the function:

    Feeble aFeeble;
    (aFeeble.*pf1)();

    I guess that's what you've been saying all along: you can assign pointers to the non-static member functions before objects exist, but you can't execute the function unless you use an object.
    Last edited by 7stud; 11-02-2005 at 02:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. dynamic array of base class pointers
    By Corrington_j in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2003, 04:58 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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