Question : Member Function Binding : Static Functions

Hey again :) [Followup question]

Okay, so I have some sample code working based on Infinity08's suggestions on poniters to member functions (just knocked it up quick to make sure it would compile).  It works great, printing a static counter value from both instances of the class to show the method is callilng from the different class instances.

Anyway, so I can now call the member function on any instance of ClassA from the same pointer with the syntax (instance2.*ptr)();.

Now, can someone show me the syntax for the following (fill in the two comments in main() with code please!) :)

Thank you!

class ClassC
{
    public:
        static void Operation1() { printf("Operation1 executed."); }
};

int main()
{
    //Make a pointer to ClassC's Operation 1.
    //Execute ClassC instance's Operation1 via the pointer.
}

Answer : Member Function Binding : Static Functions

Just treat a static class function as a free standing function.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
#include <cstdio>

class ClassC
{
    public:
        static void Operation1() { printf("Operation1 executed."); }
};

typedef void (*func_t)();

int main()
{
	func_t func = &ClassC::Operation1;
	func();
}
Random Solutions  
 
programming4us programming4us