Passing member function that accepts void *

I am working on code that needs to send a member function pointer to a logger method that accepts a void * as the parameter. I cannot change it from void *. I cannot use c++11 either. Is there a way to get it to work without any warning. For example:

logger.h

    #ifndef _LOGGER_H
    #define _LOGGER_H

    void logger( void *func );

    #endif /* _LOGGER_H */

logger.cpp

    #include <cstdio>
    #include "logger.h"

    void logger( void *func )
    {
        printf("%lx\n", (unsigned long)func);
    }

testCase.cpp

    #include "logger.h"

    class myClass
    {
        public:
            void testCase( void );
    };

    void myClass::testCase( void )
    {
        /* This works on my compiler, but gives warning */
        /* warning: converting from 'void (myClass::*)()' to 'void*' */
        /* I know this is bad and wrong. */
        logger((void *)&myClass::testCase);

        /* This compiles without warning */
        /* But doesnt work the way I need, gives ffff*/
        void (myClass::*ptr)( void ) = &myClass::testCase;
        void *m_ptr = ptr;
        logger(m_ptr);
    }

logger.h and logger.cpp cannot be changed.

This is being run a VxWorks and I need the address to look up in the symbol table. When I try the second way I get ffff. Although I get a real address when using other compilers, its different for VxWorks.

Can you think of another way to get this to work.

References

Answers


No, you can't make it happen. Standard prohibits converting pointers-to-members to void*. The reason for this is their incompatibility with void* - they are usually double the size of the void*.

Your code has other issues as well, for example, (unsigned long)func is converting a void* to unsigned long, and this is undefined as well. For example, on many systems long is 32 bits in length, while void* is 64bit. To reliably convert void* to integer type, you need to use uintptr_t (provided your implementation has it).


I don't know about "double the size"; pointers are pointers and in my understanding, all pointers on a given system are the same size, regardless of what they point to...

However, looking in my copy of The C++ Programming Language, 3rd Edition, by Stroustrup (page 101):

Occurrances of void*s at higher levers of the system should be viewed with suspicion because they are likely indicators of design errors. Where used for optimization, void* can be hidden behind a type safe interface (§13.5, §24.4.2).

Pointers to functions (§7.7) and pointers to members (§15.5) cannot be assigned to void*s.


Need Your Help

asp.net validate textbox - at least one text box must have data in

asp.net textbox validation

I have three textboxes and I want to validate them. At least one textbox must contain data.