Classes...

Started by
2 comments, last by Greg K 22 years, 2 months ago
  
#include "iostream.h"

class MyClass
{
private:
	int t;
public:
	void MyFunc( void ){ cout << "HI"; }
};


void main( void )
{
	void (MyClass:: *pFunc)( void );
	pFunc = MyClass::MyFunc;
}
  
First of all, what does this do. Second of all, how do I make a pointer to the MyFunc funtion? Thanks.
Advertisement
that code really doesn''t do anything. pointers to class functions (also called "methods") are rarely used and ugly, i dont know why. only static methods can be called without an object, so you can''t do anything with that fuction unless you create an instance of the class.

(http://www.ironfroggy.com/)
(http://www.ironfroggy.com/pinch)
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
not totally true there is an operator to call a member function of an object :

    class A{          void Test();};typedef void (A::*A_FUNCTION(void));A a;A* aPtr;A_FUNCTION func = &A::Test;//calling with instance see the little . before the *a.*func();//calling with a pointera->*func();  



Now that was ugly!!!! But he C++ standard provides with a nice object to hide all the uglyness, it is called mem_funt_t

the next piece of code does exactly has above

        #include <functionnal>std::mem_fun_t function( &A::Test );A a;function(&a);    



mem_fun_t is very usefull for doing callbacks to objects.

Edited by - Gorg on February 5, 2002 2:14:56 AM
Thanks.

This topic is closed to new replies.

Advertisement