1 Replies - 1313 Views - Last Post: 19 April 2007 - 11:23 PM Rate Topic: -----

#1 born2c0de   User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Pointer to a Member Function of a Class

Posted 19 April 2007 - 09:43 PM

I'm aware of how pointers can be used to call functions.
But I'm not exactly sure as to how a pointer can be used to call a member function of a class.

Any ideas?
Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: Pointer to a Member Function of a Class

#2 Xing   User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Pointer to a Member Function of a Class

Posted 19 April 2007 - 11:23 PM

Code for pointer to function
#include <iostream>
void display();


int main()
{
	void (*ptr)()=display;
	ptr();
	return 0;
}

void display()
{
	std::cout<<"Display";
}



Code for pointer to member function. Try to relate now.
#include <iostream>

class MyClass
{
public:
	void display()
	{
		std::cout<<"Display";
	}
};

int main()
{
	void (MyClass::*ptr)()=&MyClass::display;
	MyClass object;
	(object.*ptr)();
	return 0;
}

This post has been edited by Xing: 19 April 2007 - 11:25 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1