Thread: Member Function Pointers - Typedef vs. Not

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Member Function Pointers - Typedef vs. Not

    In my code I am going to use a typedef for the readability and whatnots, but I like knowing the intricaces of what a type really is, and am wondering why one of these compiles and one does not.

    Code:
        blooz ( bleez::* r ) ( void ) const [ 4 ] =
        {
            &blaz::doo,
            &blaz::boo,
            &blaz::roo,
            &blaz::koo
        };
        
    
        typedef blooz ( bleez::*T ) ( void ) const;
        T t [ 4 ] =
        {
            &blaz::doo,
            &blaz::boo,
            &blaz::roo,
            &blaz::koo
        };
    The first one (non-typedef'd) gives me the errors:

    Code:
    Error	1	error C2090: function returns array
    
    Error	2	error C2440: 
        'initializing' : cannot convert from 
            'CPoint (__thiscall RotatingPong::CRotatingSquare::* )(void) const' to 
            'CPoint *(__thiscall RotatingPong::CRotatingSquare::* )(void) const'

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
        blooz ( bleez::* r ) ( void ) const [ 4 ] =
    Shouldn't the first one be more like this?
    Code:
        blooz ( bleez::* r [ 4 ] ) ( void ) const  =
    A minimal compilable example always helps me come up with a better answer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Yeah, that's how it should be. I'll try to come up with some of them 'compilable examples' for the future. Many thanks.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    30
    try
    Code:
        blooz ( bleez::* r [ 4 ] ) ( void ) const =
    ah, they beat me to it

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I do that stuff so often. I just leave like 20 windows open, and then remember I was going to reply to something, and then I do only to realize that someone had answered the question like an hour before me. If you watch me closely, you'll see that I delete quite a few posts (That or Ken does. Sorry Ken, my brain is sick).

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but I like knowing the intricaces of what a type really is
    I certainly agree for data, but function pointers are special for two reasons.

    1. The syntax for declaring them is pretty horrible, especially for more complex cases. Having to repeat that all over the place makes for hard to read and maintain code.

    2. There are only ever two things you can do with a function pointer - assign it, or call it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM