Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

How to use a pointer to a member function of a class

$
0
0
#include <stdio.h>
#include <IOSTREAM>

class TestA
{
public:
	int __stdcall SayHello(int person);
	static int __stdcall SayHaha(bool laugh);
};

int TestA::SayHello(int person)
{
	printf("hello world!%d",person);
	return 0;
}
int TestA::SayHaha(bool laugh)
{
	printf("Ha haha!%d",laugh);
	return 0;
}


int (__stdcall TestA::*CallSayHello)(int)=&TestA::SayHello;
int (__stdcall *CallSayHaha)(bool)=&TestA::SayHaha;


typedef int (__stdcall TestA::*XSayHello)(int);
XSayHello hello;
typedef int (__stdcall *XSayHaha)(bool);
XSayHaha haha;

int main()
{
	//(*CallSayHello)(1);
	(*CallSayHaha)(true);
	//hello(1);
	haha(true);
	system("pause");
	return 0;
}

How to use "CallSayHello" and "hello" in function "main".

Follow code works:

	__asm
	{
		push eax;
		mov eax,dword ptr TestA::SayHello;
		mov dword ptr hello,eax;
		pop eax;
	}

butmethod performs the complete quote errors: "Runtime check failture #0 ... "In my project, I need to be in invoked without creating an object of theclass type of the method, because I could not create objects of this class.Could you please tell me how to do it? I need some more C++ similar toimplement callbacks in c # through the event information. Thank you.


zhuangtaiqiusi



Viewing all articles
Browse latest Browse all 15302

Trending Articles