Error in array initialization in managed C ++ (continued)

Following on from my previous question.

Can someone explain why the following code compiles without error:

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)
};

      

However, this gives me "error C2440: 'initializing': cannot convert from 'VdbParameterInfo ^' to '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 did was try to "nest" the array of parameters into the initialization of the array of methods ... Unwise - the VdbMethodInfo constructor takes a ParameterArray parameter as its second argument.

It looks like C ++ 's managed array initialization expects any recursive nesting to be of the same type ... (i.e., I think it must be a bug)

Related question: here

+1


a source to share


1 answer


I found a workaround that does the syntax filter anyway. I use the syntax "..." (the C ++ managed equivalent of the C # "params" keyword):



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;
    }
};

      

+2


a source







All Articles