繁体   English   中英

这可能吗? 在C#中调用托管C ++结构构造函数

[英]Is this possible? Calling managed c++ struct constructor in C#

我有一个托管的c ++类/结构,带有接受输入的构造函数。 在C#中,我只能“看到”默认构造函数。 有没有一种方法可以在不离开托管代码的情况下调用其他构造函数? 谢谢。

编辑:实际上,其功能均不可见。

C ++:

public class Vector4
{
private:
    Vector4_CPP test ;

    Vector4(Vector4_CPP* value)
    {
        this->test = *value;
    }


public:
    Vector4(Vector4* value)
    {
        test = value->test;
    }
public:
    Vector4(float x, float y, float z, float w)
    {
        test = Vector4_CPP( x, y, z, w ) ;
    }


    Vector4 operator *(Vector4 * b)
    {
        Vector4_CPP r = this->test * &(b->test) ;
        return Vector4( &r ) ;
    }
} ;

C#:

// C# tells me it can't find the constructor.
// Also, none of them are visible in intellisense.
Library.Vector4 a = new Library.Vector4(1, 1, 1, 1);

第一个问题是您的类声明是针对非托管C ++对象的。

如果需要托管的C ++ / CLI对象,则需要以下之一:

public value struct Vector4

要么

public ref class Vector4

同样,任何包含本机类型的C ++ / CLI函数签名对于C#都是不可见的。 因此,任何参数或返回值都必须是C ++ / CLI托管类型或.NET类型。 我不确定operator *签名的外观,但是您可以这样休息:

public value struct Vector4 
{   
  private:
    Vector4_CPP test;

    Vector4(Vector4_CPP* value)
    {
        this->test = *value;
    }

  public:
    Vector4(Vector4 value)
    {
        test = value.test;
    }

    Vector4(System::Single x, System::Single y, System::Single z, System::Single w)
    {
        test = Vector4_CPP( x, y, z, w ) ;
    } 
}

要么:

public ref class Vector4 
{   
  private:
    Vector4_CPP test;

    Vector4(Vector4_CPP* value)
    {
        this->test = *value;
    }

  public:
    Vector4(Vector4^ value)
    {
        test = value->test;
    }

    Vector4(System::Single x, System::Single y, System::Single z, System::Single w)
    {
        test = Vector4_CPP( x, y, z, w ) ;
    } 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM