Archived

This topic is now archived and is closed to further replies.

Classes...

This topic is 5825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

  
#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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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 pointer

a->*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

Share this post


Link to post
Share on other sites