C ++在C#中的工会 [英] C++ union in C#

查看:121
本文介绍了C ++在C#中的工会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我翻译写在C ++到C#图书馆和关键字联盟存在一次。在一个结构。

什么是翻译成C#的正确方法?而且它有什么作用?它看起来是这样的;

 结构美孚{
    浮杆;    工会{
        INT killroy;
        浮FUBAR;
    }作为;
}


解决方案

您可以使用显式现场布局是:

  [StructLayout(LayoutKind.Explicit)
公共结构SampleUnion
{
    [FieldOffset(0)公众持股量栏;
    [FieldOffset(4)公众诠释killroy;
    [FieldOffset(4)公众持股量FUBAR;
}

未经检验。这个想法是,两个变量中的结构相同的位置。你当然可以只使用其中的一个。

有关工会更多信息结构教程

I'm translating a library written in C++ to C#, and the keyword 'union' exists once. In a struct.

What's the correct way of translating it into C#? And what does it do? It looks something like this;

struct Foo {
    float bar;

    union {
        int killroy;
        float fubar;
    } as;
}

解决方案

You can use explicit field layouts for that:

[StructLayout(LayoutKind.Explicit)] 
public struct SampleUnion
{
    [FieldOffset(0)] public float bar;
    [FieldOffset(4)] public int killroy;
    [FieldOffset(4)] public float fubar;
}

Untested. The idea is that two variables have the same position in your struct. You can of course only use one of them.

More informations about unions in struct tutorial

这篇关于C ++在C#中的工会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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