所以我在得到的 C++ 头文件中有这个函数原型(prototype):
extern LIBRARY_API BOOL read(unsigned int in_one, unsigned int & value);
但我通过 MATLAB mex 文件运行它,因此它可能必须是 C 语言,而不是 C++ 语言。由于引用是 C++ 独有的东西,有人建议将函数原型(prototype)更改为指针参数。那么我可能会得到这样的东西:
extern LIBRARY_API BOOL read(unsigned int in_one, unsigned int * value);
然后在 mex 文件中,我将确保创建了 unsigned int *value 而不是 unsigned int value 并在运行读取函数后取消引用它。
但是,我担心(1)我可能引用/取消引用一件事太多(或不够),并且(2)我无法做到这一点,因为我无法更改实际的源代码并更改原型(prototype)只会导致实例化和定义之间的不匹配。
所以假设我可以以某种方式更改定义以匹配 header ,我上面的指针函数是否是有效的替换?如果我无法更改源代码,是否可以用 C 进行替代?就像仍然允许相同定义的引用 C 替代吗?
最佳答案
So assuming that I can somehow change the definition to match the header, would my pointer function above be a valid substitution?
是的。
您只需在调用站点传递 &myInt
而不是 myInt
即可。
And if I can't change the source code, is there a substitution that I could make that would be possible with C? Like a reference C-substitute that would still allow for the same definition?
没有。
如果 MATLAB 需要 C 调用约定等,那么您就只能使用 C 功能(大部分)。
就其值(value)而言,我相信 MATLAB 可以很好地使用 C++; the documentation通篇指的是“C/C++”(我们必须假设,它们的意思是“C 和 C++”)。
所以,看看您问题中的最后一句话:
But I'm running it via a MATLAB mex file, so it may have to be in C, not C++. Since references are a C++ only thing, someone suggested changing
停下来。首先看看它是否真的按原样工作。从那里继续。
关于c++ - 我可以更改将引用参数传递到指针的函数的输入并仍然使其工作(C、C++)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45064275/