【问题标题】:Is the 'explicit' keyword necessary and/or recommended for WinRT classes?WinRT 类是否需要和/或推荐使用“显式”关键字?
【发布时间】:2014-07-01 16:38:20
【问题描述】:

在 WinRT 中构造类型时,explicit 关键字是否具有与标准 C++ 类型相同的功能和价值? IE。 WinRT ref 类型是否支持与标准 C++ 类型相同的隐式转换?

#pragma once

#include <collection.h>
#include <ppltasks.h>

using namespace Platform;

namespace RuntimeComponent2
{
    public ref class Class1 sealed
    {
    public:
        explicit Class1(Platform::String^ foo); // does the 'explicit' keyword here have any value?
    private: 
         Platform::String^ m_foo;
    };

    Class1::Class1(Platform::String^ foo)
        : m_foo(foo)
    {
    }
}

【问题讨论】:

    标签: windows-runtime c++-cx


    【解决方案1】:

    我不是 C++/CX 方面的专家,但 AFAIK 引用类只能通过 ref new 实例化。隐式转换 otoh 只能通过创建(概念上)驻留在堆栈上的对象来进行。

    因此,如果我对 ref new 的看法是正确的,则 ref class 对象不会发生隐式转换,并且 explicit 关键字在它们的构造函数(和 C++11 转换运算符)中没有用处。

    【讨论】:

    • 具体来说,C++/CX 中的 ctors(非默认)被转为类工厂方法,所以语法有很多谎言。他们不是真正的演员。
    猜你喜欢
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 2014-12-17
    • 2013-11-27
    • 2023-03-26
    相关资源
    最近更新 更多