分配本机C ++结构的CX公共值结构内容

时间:2017-06-30 01:05:17

标签: c++ struct c++-cx

如果我有两个这样的结构:

struct A {
    float a;
    float b;
    float c;
    float d;
}

public value struct B {
    float a;
    float b;
    float c;
    float d;
}

从A转换为B的最佳方法是什么(反之亦然)。我可以这样做:B struct_b = static_cast<B>(A{1,2,3,4})吗?

1 个答案:

答案 0 :(得分:0)

支持的方法是通过转换运算符:

struct A;

public value struct B {
    float a;
    float b;
    float c;
    float d;

internal:
    operator A();
};

struct A {
    float a;
    float b;
    float c;
    float d;

    operator B()
    {
        return B{ a, b, c, d };
    }
};

B::operator A()
{
    return A{ a, b, c, d };
}

请注意,Intellisense可能会抱怨operator A()有一个红色波浪形和一个关于“公共非数据成员”的工具提示但你可以忽略它并且代码将编译。 internal关键字在C ++中表示“public”,但在WinMD中不作为元数据的一部分公开。“