C++/CLI和C#:对象返回自身 给定类对象< /> >,C++中可能返回对对象本身的引用,如: //C++ class Object { Object& method1() { //.. return *this; } Object& method2() { //. return *this; } }

C++/CLI和C#:对象返回自身 给定类对象< /> >,C++中可能返回对对象本身的引用,如: //C++ class Object { Object& method1() { //.. return *this; } Object& method2() { //. return *this; } },c#,c++-cli,C#,C++ Cli,然后将其消费为: //C++ Object obj; obj.method1().method2(); 是否可以在C++/CLI中实现同样的效果,并在C#应用程序中使用它?我尝试了以下方法(使用refs%和handles^),它们是用C++/CLI编译的,但C#说这样的方法是 该语言不支持 然后用作: //C# Object obj = new Object(); obj.method1(); //ERROR obj.method1().

然后将其消费为:

    //C++
    Object obj;
    obj.method1().method2();
是否可以在C++/CLI中实现同样的效果,并在C#应用程序中使用它?我尝试了以下方法(使用refs
%
和handles
^
),它们是用C++/CLI编译的,但C#说这样的方法是

该语言不支持

然后用作:

    //C#
    Object obj = new Object();
    obj.method1(); //ERROR
    obj.method1().method2(); //ERROR
//C#
Object obj = new Object();
obj.method1();
obj.method1().method2();

谢谢

对于C++/CLI,您只需要以下内容:

public ref class Object
{
public:
    Object ^method1()
    {
        //..
        return this;
    }
    Object ^method2()
    {
        //.
        return this;
    }
};
好的,这很好:

//C++/CLI - compiles OK
public ref class Object
{
    Object^ method1()
    {
        //..
        return this;
    }
    Object^ method2()
    {
        //.
        return this;
    }
}
然后用作:

    //C#
    Object obj = new Object();
    obj.method1(); //ERROR
    obj.method1().method2(); //ERROR
//C#
Object obj = new Object();
obj.method1();
obj.method1().method2();

谢谢@Dave,我也明白了:)把你的答案标记为正确!