Allows you to add a static member to the class as:
class foo{ public: void bar() { cout<<"hey there"<<endl; } static void bar(foo*) { cout<<"STATIC MEMBER"<<endl; } };
Now if you write this:
foo::bar(&obj); //static or non-static?
What function should be called? In such a situation, what would you call them both? What will be the syntax? If you allow one function to have this syntax, you must abandon it (for example, syntax) for another function. The standard decided to have the syntax foo :: bar (& obj) for the static member function, leaving it for the non-static member function.
In any case, if you want to pass &obj as an argument to a non-static member function, you can use type erase, simplified by std::function , like:
void (foo::*pbar)() = &foo::bar; //non-static member function
Similarly, you can call a static member function as:
void (*pbar)(foo*) = &foo::bar; //static member function
Note that in lines #1 and #2 types of the pbar object force the compiler to select the correct member function - in the first case, it takes a pointer to a non-stationary member function, and in the latter case, it takes a pointer to a static member function.
Hope this helps.
source share