VishalNikiSharma
March 12th, 2003, 03:53 AM
Dear Gurus
i have come across a term function pointer what it is and how to use it
Thanks in advance
Vishal
i have come across a term function pointer what it is and how to use it
Thanks in advance
Vishal
|
Click to See Complete Forum and Search --> : function pointer VishalNikiSharma March 12th, 2003, 03:53 AM Dear Gurus i have come across a term function pointer what it is and how to use it Thanks in advance Vishal dude_1967 March 12th, 2003, 04:02 AM VNS, A function pointer is a pointer to a statically defined function, where the function has a physical address. It is relatively simple to create a data-type for function pointers and to use such data-types in simple operations. There are many uses for function pointers and it is possible to have all kinds of function pointers for functions with other return values and input parameters. Look at the simple sample below which defines a function pointer to void f(void). #include <iostream> // Function pointer: This is a pointer to a void function taking void. typedef void (*PFN)(void); void test(void) { ::std::cout << "Hello World!" << ::std::endl; } int main(int argc, char* argv[]) { // Use of a function pointer PFN pfn = test; pfn(); return 1; } Kheun March 12th, 2003, 06:56 PM In addition to that, it also works for public member function within a class. in .h file class MyClass; typedef void (MYClass::*memFunc)() const; class MyClass { public: void foo() const; void bar() const; void display(int n); static memFunc func[]; } in .cpp file memFunc MyClass::func[] = {MyClass::foo, MyClass::bar}; void MyClass::foo() const { cout << "in foo" << endl; } void MyClass::bar() const { cout << "in bar" << endl; } int main() { MyClass a; (a.*MyClass::func[0])(); // Call foo (a.*MyClass::func[1])(); // Call bar return 0; } codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |