托管C ++中的数组初始化中的错误(后续)

时间:2022-09-01 11:27:45

Following up from my previous question.

跟进我之前的问题。

Can anyone explain why the following code compiles without any errors:

任何人都可以解释为什么以下代码编译没有任何错误:

typedef array<VdbMethodInfo^> MethodArray;
typedef array<VdbParameterInfo^> ParameterArray;
ParameterArray^ parameters = gcnew ParameterArray {
    gcnew VdbParameterInfo("name", "string", "Paul")};
MethodArray^ methods = gcnew MethodArray {
    gcnew VdbMethodInfo("createTable", parameters)
};

Yet this gives me "error C2440: 'initializing' : cannot convert from 'VdbParameterInfo ^' to 'VdbMethodInfo ^"

然而,这给了我“错误C2440:'初始化':无法从'VdbParameterInfo ^'转换为'VdbMethodInfo ^”

typedef array<VdbMethodInfo^> MethodArray;
typedef array<VdbParameterInfo^> ParameterArray;
MethodArray^ methods = gcnew MethodArray {
    gcnew VdbMethodInfo("createTable", gcnew ParameterArray {
        gcnew VdbParameterInfo("name", "string", "Paul")};
    )
};

All I've done is attempt to "nest" the parameter array inside the method array initialization... Not directly mind - VdbMethodInfo's constructor takes, as a second argument, a ParameterArray.

我所做的就是尝试在方法数组初始化中“嵌套”参数数组...不直接介意 - VdbMethodInfo的构造函数将ParameterArray作为第二个参数。

It seems to imply that managed C++ array initialization expects any recursive nesting to have the same type... (i.e. I think this must be a bug)

这似乎意味着托管C ++数组初始化期望任何递归嵌套具有相同的类型...(即我认为这必定是一个错误)

Related question : here

相关问题:这里

1 个解决方案

#1


I've found a workaround which makes the syntax cleaner anyway. I use the "..." syntax (Managed C++ equivalent to the C# "params" keyword"):

我找到了一种解决方法,无论如何都会使语法更清晰。我使用“...”语法(托管C ++相当于C#“params”关键字“):

public ref class MetaData
{
    typedef array<VdbMethodInfo^> MethodArray;
    typedef array<VdbParameterInfo^> ParameterArray;
    static ParameterArray^ params(... ParameterArray^ p)
    {
        return p;
    }
public:
    static array<VdbMethodInfo^>^ Instance()
    {
        ParameterArray^ parameters = gcnew ParameterArray { gcnew VdbParameterInfo("name", "string", "Paul")};
        MethodArray^ methods = gcnew MethodArray {
            gcnew VdbMethodInfo("createTable",
                params(gcnew VdbParameterInfo("name", "string", "Paul"),
                       gcnew VdbParameterInfo("age", "number", "25")))
        };

        return methods;
    }
};

#1


I've found a workaround which makes the syntax cleaner anyway. I use the "..." syntax (Managed C++ equivalent to the C# "params" keyword"):

我找到了一种解决方法,无论如何都会使语法更清晰。我使用“...”语法(托管C ++相当于C#“params”关键字“):

public ref class MetaData
{
    typedef array<VdbMethodInfo^> MethodArray;
    typedef array<VdbParameterInfo^> ParameterArray;
    static ParameterArray^ params(... ParameterArray^ p)
    {
        return p;
    }
public:
    static array<VdbMethodInfo^>^ Instance()
    {
        ParameterArray^ parameters = gcnew ParameterArray { gcnew VdbParameterInfo("name", "string", "Paul")};
        MethodArray^ methods = gcnew MethodArray {
            gcnew VdbMethodInfo("createTable",
                params(gcnew VdbParameterInfo("name", "string", "Paul"),
                       gcnew VdbParameterInfo("age", "number", "25")))
        };

        return methods;
    }
};