C+中的转换运算符+/Cli并在c#中使用它(使用as运算符),并使用c++;在c中#

C+中的转换运算符+/Cli并在c#中使用它(使用as运算符),并使用c++;在c中#,c#,string,c++-cli,type-conversion,C#,String,C++ Cli,Type Conversion,如何在c++\cli中实现转换运算符以在c#中使用此运算符;使用“as”运算符 这是我在C#中的测试方法: 我尝试使用: explicit static operator Square(Cube^ cube) { Square ne(cube->GetSize()); return ne; } 但它不起作用 我的方法实现 Square::Square() {} Square::Square(double a) { _size = a; } Square::Sq

如何在c++\cli中实现转换运算符以在c#中使用此运算符;使用“as”运算符

这是我在C#中的测试方法:

我尝试使用:

explicit static operator Square(Cube^ cube)
{
    Square ne(cube->GetSize());
    return ne;
}
但它不起作用

我的方法实现

Square::Square() {}

Square::Square(double a)
{
    _size = a;
}

Square::Square(const Square% val) // copy constructor
{
    _size = 4;
}

Square::~Square() {}

double Square::CountPerimeter()
{
    return 4 * _size;
}


Cube::Cube() {}

Cube::Cube(double a)
{
    _size = a;
}

Cube::Cube(const Cube% val)
{
    _size = 4;
}

Cube::~Cube() {}

double Cube::CountPerimeter()
{
    return 12 * _size;
}
(二) 我还有第二个问题。我想在C++中使用C++中的字符串 c语言实现++

#include <iostream>

string Square::Introduce()
{
    return "I'm a square.";
}
#包括
stringsquare::介绍()
{
返回“我是一个正方形。”;
}

以后,请问两个不同的问题

请不要说“它不工作”。这是否意味着编译错误?运行时错误?天网是在你的电脑上自发进化的吗?请不要让我们猜

也就是说

  • is
    as
    不注意用户定义的转换。您必须直接向 Suth
  • 始终使用.Net字符串。这是
    String^
    ,大写字母为“s”,克拉。如果需要与使用其他内容的C/C++代码进行互操作,请使用.Net字符串,直到需要调用C/C++,然后再进行转换

  • 请问两个不同的问题。如果你同时问两个问题,很难提供一个“最佳”答案。不要返回STD::String,只有C++编译器知道这意味着什么。返回System::String^。并发布复制代码,以便用户可以复制/粘贴以诊断您的问题。永远不要记录“它不起作用”。
    [Test method]
    public void Test2()
    {
        Assert.AreEqual("I'm a square.", s.Introduce());\
    }
    
    #include <iostream>
    
    string Square::Introduce()
    {
        return "I'm a square.";
    }