[英]C++/CLI and C#: object returns itself
Given a class Object
, it's possible in C++ to return a reference to the object itself like: 给定一个
Object
类,在C ++中可以返回对对象本身的引用,如下所示:
//C++
class Object
{
Object& method1()
{
//..
return *this;
}
Object& method2()
{
//.
return *this;
}
}
And then consume it as: 然后将其用作:
//C++
Object obj;
obj.method1().method2();
Is it possible to achive the same effect in C++/CLI and use it in C# application? 是否有可能在C ++ / CLI中达到相同的效果并在C#应用程序中使用它? I've tried the following (with refs
%
and handles ^
), which compiles in C++/CLI, but C# says that such methods are 我已经尝试了以下内容(使用refs
%
和handles ^
),它们在C ++ / CLI中进行编译,但是C#表示这样的方法是
is not supported by the language
该语言不支持
//C++/CLI - compiles OK
public ref class Object
{
Object% method1()
{
//..
return *this;
}
Object% method2()
{
//.
return *this;
}
}
Then use as: 然后用作:
//C#
Object obj = new Object();
obj.method1(); //ERROR
obj.method1().method2(); //ERROR
Thanks 谢谢
You just need the following for C++/CLI: 对于C ++ / CLI,您只需要以下内容:
public ref class Object
{
public:
Object ^method1()
{
//..
return this;
}
Object ^method2()
{
//.
return this;
}
};
OK, this worked fine: 好的,这很好用:
//C++/CLI - compiles OK
public ref class Object
{
Object^ method1()
{
//..
return this;
}
Object^ method2()
{
//.
return this;
}
}
Then use as: 然后用作:
//C#
Object obj = new Object();
obj.method1();
obj.method1().method2();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.