如何从不同的类调用函数指针 [英] How to call function pointers from different classes

查看:77
本文介绍了如何从不同的类调用函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++/CLI项目,该项目具有:
1)内部定义了函数指针的非托管结构

I have a C++/CLI project which has:
1) An unmanaged struct with function pointers defined inside it

typedef void (*FunctionPointer1)(int *a);
typedef void (*FunctionPointer2)(int a);
struct MyStruct{
FunctionPointer1 f1;
FunctionPointer2 f2;
};



2)我需要从类1调用f1,从类2调用f2
3)现在,我已经定义了一个用于存储struct地址的辅助类(不能存储值,因为编译器抱怨在托管类中使用非托管结构),如下所示:



2) I need to call f1 from class 1 and f2 from class 2
3) Right now I have defined a helper class which stores the address of the struct( cannot store the values as the compiler complains of using unmanaged struct in managed class) as follows:

public ref class Helper abstract sealed
{
public:
static property MyStruct *MYSTRUCT
{
MyStruct *get();
void set(MyStruct *value);
}

private:
static MyStruct *myStruct;
};


4)类1通过在Helper类中调用setter来存储Mystruct的值,而类2通过调用getter来获取它的值

但是,当我在Class 2中调用函数f2时,我没有得到预期的行为.有人可以帮助我吗?为什么结构的地址不能正确检索函数指针?是否由于Helper类中属性的静态性质?还是有更好的方法?


4) Class 1 stores the value of Mystruct by calling the setter in Helper class and Class 2 retrieves it by calling the getter

But I do not get the expected behavior when I call the function f2 in Class 2. Can anyone help me with this? Why is the address of the struct not able to retrieve the function pointers correctly? Is it because of the static nature of the properties in the Helper class? Or is there a better way of doing this?

推荐答案

我已经以某种方式修改了您的解决方案.该属性不再是静态的,并且由于getter/setter无关紧要,因此我使用了自动实现的属性.

我还向助手添加了函数,以封装函数调用.最好这样做,因为这样可以更好地分隔托管代码和非托管代码……因此,如果已经创建了帮助程序,则可以使用其他语言(例如C#或VB.NET)中的帮助程序. >
而且我添加了一些功能来测试代码...

I have somehow modified your solution. The property is not static anymore and since the getter/setter are trivial, I have used auto-implemented properties.

I have also added functions to the helper to encapsulate function calls. This is preferable to do it that way as you have a better separation between managed and unmanaged code... thus it would be possible to use the helper from another language like C# or VB.NET if the helper is already created.

And I have added some functions to test the code...

#include "stdafx.h"

#include <iostream>
#include <string>

typedef void (*FunctionPointer1)(int *a);
typedef void (*FunctionPointer2)(int a);

struct MyStruct{
	FunctionPointer1 f1;
	FunctionPointer2 f2;
};

public ref class Helper
{
public:
	Helper(MyStruct const &a)
	{
		MYSTRUCT = new MyStruct(a);
	}

        // Destructor could be made virtual if necessary.
	~Helper()
	{
		Helper::!Helper();
	}

	!Helper()
	{
		delete MYSTRUCT;
	}

	void CallF1(int *a) { (MYSTRUCT->f1)(a); }
	void CallF2(int a) { (MYSTRUCT->f2)(a); }

	property MyStruct *MYSTRUCT;
};

void f1a(int *a) 
{
	std::cout << "f1a(int *)" << std::endl;
}

void f1b(int *a) 
{
	std::cout << "f1b(int *)" << std::endl;
}

void f2a(int a) 
{
	std::cout << "f2a(int)" << std::endl;
}

void f2b(int a) 
{
	std::cout << "f2b(int)" << std::endl;
}

int main(array<System::String ^> ^args)
{
	MyStruct msa;
	msa.f1 = &f1a;
	msa.f2 = &f2a;

	MyStruct msb = { &f1b, &f2b };

	int z = 123;
	Helper ^ha = gcnew Helper(msa);
	ha->CallF1(&z);
	ha->CallF2(z);

	Helper ^hb = gcnew Helper(msb);
	hb->CallF1(&z);
	hb->CallF2(z);

	// You can delete the object to release unmanaged 
	// resources before garbage collection
	delete hb;

    return 0;
}


这篇关于如何从不同的类调用函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆