Search code examples
c#c++c++-cli

Implement C# interface function returning array, in C++/CLI


I have a C++/CLI project that references a C# dll, and I need to implement an Interface class.

The interface class in C# looks like this:

public interface IMeshData{
    //(...)        
    INode NodeById(int nodeId);
    double[] NodeXYZById(int nodeId);
}

In the C++ project I can implement this interface:

public ref class IMeshData_class : public IMeshData{

public:
    //this one is accepted:
    virtual INode^ NodeById(int nid){
        return this->NodeById(nid);
    }
    //this one gives me an error:
    virtual double*  NodeXYZById(int nid){
         return this->NodeXYZById(nid);
    }
}

When I first define my class above without any member function, I get the error:

Error: class fails to implement interface member function "IMeshData::NodeById" (declared in (...).dll)

So after defining the function NodeById, this error goes away, and I get:

Error: class fails to implement interface member function "IMeshData::NodeXYZById" (declared in (...).dll)

NodeXYZById return a double[], I thought this would translate to a double* in C++, but it doesn't seem to be the case.

How should I correctly implement a C# member function in C++ which returns an array?


Solution

  • As commented above, in C++/CLI you have to use the type array<T>^ in place of what might be T[] in C#.