C++ gets the address of the class member function and calls the function through the address

class BaseWithoutV {
    
    
public:
	void func1()
	{
    
    
		MessageBox(0, "BaseWithoutV func1", "", 0);
	}
	void func2()
	{
    
    
		MessageBox(0, "BaseWithoutV func2", "", 0);
	}
private:
	int m_data1, m_data2;
};

int main()
{
    
    
	// 这里打印的可能不是 MessageBox 的上层函数,因为  &BaseWithoutV::func1 可能是一个跳板函数(也就是 MessageBox 的上上层函数)
	printf("func1 address : %X \n", &BaseWithoutV::func1);
	
	typedef void (BaseWithoutV:: * fun)();
	fun f = &BaseWithoutV::func1;
	BaseWithoutV bwv;
	(bwv.*f)();

	return 0;
}

Guess you like

Origin blog.csdn.net/Simon798/article/details/113701038