Hi I'm trying to create and marshal the following structure from C# into C++ and maintain the linked reference. I'm unsure how this structure should be defined in C#? In C++ the structure must look like below, with the const reference maintained.
// C++
struct {
int a; // my value
const int& b = a; // my refere开发者_开发问答nce to a
}
Does anyone know if this is possible?
Thanks.
Edit:
This is more representative of what I'm trying to accomplish, and as pointed out by @Hans it is not legal C++, but maybe someone can suggest a better path? The system_t
is generated in either C++ or C# and passed to C++. My best guess: (if this is even a good design pattern) is to initialize all the references in the system_t
constructor in C++. As far as marshaling from C#, it will get complicated.
struct system_t
{
float sysSampleRate = 112500.0f; // Sample rate from receivers.
// Illegal statement @Hans
struct tvg_t // tvg_t is passed to tvg processor
{
float tvgLinearGain;
const float& tvgSampleRate = sysSampleRate; // Get the rate from system.
// Illegal statement @Hans
} tvg; // Nested tvg_t in system_t.
//... Many other structures and variables ..//
};
I'd like to find the right design pattern rather than abandoning this and going to a flat structure or passing system_t
to every module.
This should work:
[StructLayout(LayoutKind.Sequential)]
public struct MyCStruct {
public int a;
public IntPtr b;
}
精彩评论