Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Comparing Member Function Pointers

In C++, is it possible to define a sort order for pointers to member functions? It seems that the operator< is undefined. Also, it's illegal to cast to void*.

class A
{
    public:
        void Test1(){}
        void Test2(){}
};

int main()
{
    void (A::* const one)() = &A::Test1;
    void (A::* const two)() = &A::Test2;

    bool equal = one == two; //Equality works fine.
    bool less = one < two; //Less than doesn't.

    return 0;
}

Thanks!

like image 484
Imbue Avatar asked Nov 19 '09 18:11

Imbue


People also ask

Can you compare function pointers in C?

Two function pointers can be compared with the == and != operators, just like any other kind of pointers. We can also compare a function pointer to the NULL pointer using the == and !=

How do you get a pointer to member function?

The pointer to member operators . * and ->* are used to bind a pointer to a member of a specific class object. Because the precedence of () (function call operator) is higher than . * and ->* , you must use parentheses to call the function pointed to by ptf .

Can we call member function using this pointer in constructor?

the constructor is the first function which get called. and we can access the this pointer via constructor for the first time. if we are able to get the this pointer before constructor call (may be via malloc which will not call constructor at all), we can call member function even before constructor call.

What is * this pointer in C++?

The this pointer is a pointer accessible only within the nonstatic member functions of a class , struct , or union type. It points to the object for which the member function is called.


2 Answers

Function pointers are not relationally comparable in C++. Equality comparisons are supported, except for situations when at least one of the pointers actually points to a virtual member function (in which case the result is unspecified).

Of course, you can always introduce an ordering by implementing a comparison predicate and comparing the pointers explicitly (won't look too elegant though, since you can only use equality comparisons). Other possible solutions would cross into the territory of the various implementation-specific "hacks".

like image 94
AnT Avatar answered Oct 22 '22 08:10

AnT


Member function pointers are not actual pointers. You should look at them as opaque structs. What does a method pointer contain:

 struct method_pointer {
     bool method_is_virtual;
     union {
         unsigned vtable_offset; // for a virtual function, need the vtable entry
         void* function_pointer; // otherwise need the pointer to the concrete method
     }
 };

If you could cast this to void* (you can't) all you would have is a pointer the the struct, not a pointer to code. That's why operator<() is undefined as well since the value of the struct's pointer is just where ever it happens to be in memory.

In addition to that, what are you sorting by?

like image 31
jmucchiello Avatar answered Oct 22 '22 07:10

jmucchiello