c++ - 分配 native C++ 结构的 CX 公共(public)值结构内容

标签 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})

最佳答案

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

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 可能会用红色波浪线和关于“公共(public)非数据成员”的工具提示来提示 operator A(),但您可以忽略它,代码将编译。 internal 关键字表示“C++ 中的public 但不作为元数据的一部分公开在 WinMD 中。”

关于c++ - 分配 native C++ 结构的 CX 公共(public)值结构内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44837127/

相关文章:

c++ - 在 Windows Metro 中以编程方式从网格中删除对象

c++ - 不能同时旋​​转和平移我的场景 - direct3d

c++ - 从模板父类导入一个模板成员类型,无需重复名称

struct - Go Programming - 如何在 ExecuteTemplate 中传递两个结构

c# - 在 C# 中更改结构的属性值

c++ - 访问类私有(private)成员中的结构成员?

c++ - Windows 手机 C++

c++ - 使用 _crtBreakAlloc、_CRTDBG_MAP_ALLOC 追踪插件中的内存泄漏

c++ - 如果在初始化静态局部变量之前发生异常会怎样?

c++ - 如何在继承类之前初始化成员变量