Pointer to member function?

This is a discussion on Pointer to member function? within the C++ Programming forums, part of the General Programming Boards category; I'm working on some networking code. The first byte in a packet determines what kind of packet it is. For ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    69

    Pointer to member function?

    I'm working on some networking code. The first byte in a packet determines what kind of packet it is. For each kind of packet, there is a different handler function. These handler functions must be part of a certain class. For performance reasons, I don't want to use a huge switch statement, so I came up with this solution:
    There is an array of function pointers. The array has 256 elements. When I recieve a packet, I use the first byte of the packet as the index on that array, and use it to call the appropriate function. This works with normal functions, but because some of the functions must change internal properties of the networking class (like the number of connected systems when a new system joins), they must be member functions.

    When I try to create a pointer to a member function, I get the error " '&' : illegal operation on bound member function expression". I don't really understand what this means, could someone explain this to me in simple language?

    I've tried to following ways:
    internalPacketHandlers[i] = &(NetworkInterface::defaultPacketHandler);
    internalPacketHandlers[i] = &(this.defaultPacketHandler);
    internalPacketHandlers[i] = &defaultPacketHandler;
    The last one works when defaultPacketHandler is a global function.

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    The &(NetworkInterface::defaultPacketHandler) one looks correct, though you can also write it as &NetworkInterface::defaultPacketHandler.
    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
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    You create a pointer to a member function like so:
    Code:
    int pointer = (int)&(structer.member);

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    You create a pointer to a member function like so:
    I do not think so. Kindly provide a compilable example program.
    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

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    If the types are correct, then
    internalPacketHandlers[ i ] = NetworkInterface::defaultPacketHandler;
    should be just fine.
    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.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    Ok I've found the problem. I wasn't properly defining the pointer-to-member function type. I left it the same as the non-member function pointer. I think the correct code is:
    Code:
    typedef void (NetworkInterface::*InternalPacketHandler)(unsigned char* data, unsigned int dataLength, SystemAddress sourceSystem);
    Instead of:
    Code:
    typedef void (InternalPacketHandler)(unsigned char* data, unsigned int dataLength, SystemAddress sourceSystem);
    And in the class:
    Code:
    private:
    InternalPacketHandler internalPacketHandlers[256];
    In the constructor:
    Code:
    internalPacketHandlers[i] = &NetworkInterface::defaultPacketHandler;

    However, this brings me to another problem:
    The typedef of InternalPacketHandler requires that the class NetworkInterface has been defined before, while the class NetworkInterface requires that InternalPacketHandler has been defined before. How do I solve this?

    EDIT:
    just saw your post, Salem. Shouldn't it be "internalPacketHandlers[i] = &NetworkInterface::defaultPacketHandler;" instead of "internalPacketHandlers[ i ] = NetworkInterface::defaultPacketHandler;"?
    Last edited by C+/-; 05-13-2007 at 07:50 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    If the types are correct, then
    internalPacketHandlers[ i ] = NetworkInterface::defaultPacketHandler;
    should be just fine.
    hmm... I had the impression that that would work for ordinary function pointers, but not for pointers to member functions. I tested with:
    Code:
    class Bar
    {
    public:
        void foo() {}
    };
    
    int main()
    {
        typedef void (Bar::*MemFun)();
        MemFun member_functions[2];
        member_functions[0] = Bar::foo;
    }
    Did I get my types wrong? Modifying main() as shown below compiles:
    Code:
    int main()
    {
        typedef void (Bar::*MemFun)();
        MemFun member_functions[2];
        member_functions[0] = &Bar::foo;
    }
    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

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    Hmpft.
    http://www.newty.de/fpt/fpt.html#assign
    Apparently, the & is needed
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    The typedef of InternalPacketHandler requires that the class NetworkInterface has been defined before, while the class NetworkInterface requires that InternalPacketHandler has been defined before. How do I solve this?
    Would a forward declaration work?
    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

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    Thanks guys, I think I'll get it to work now. I solved my last problem with a forward declaration.

    Regarding the '&' operator, I found this: "Some compilers (most notably MSVC 6 and 7) will let you omit the &, even though it is non-standard and confusing. More standard-compliant compilers (e.g., GNU G++ and MSVC 8 (a.k.a. VS 2005)) require it, so you should definitely put it in." here: http://www.codeproject.com/cpp/FastDelegate.asp

    Thanks again! Such a wonderful forum

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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