I'm trying to locate examples of assignment operators and copy constructors in C++/CLI. I开发者_开发知识库 have spent a lot of time on Google and surprisingly I can't find a decent example of something that seems pretty common.
.NET semantics have no such thing as a copy constructor or assignment operator. You can define one in your ref class
es, but it will be used only in the C++ side if you request explicitly a copy` For value classes, everything is builtin and you cannot override copy semantics.
Example:
public ref class Foo
{
Foo(const Foo% f);
};
Foo^ f = gcnew Foo;
Foo^ g = gcnew Foo(*f); // This will call C++ copy constructor. No .NET equivalent.
Look at ICloneable
if you want to implement deep copy semantics in the .NET style.
Also look there to get the different copy behaviors you can have. I'd strongly advice against storing ref class
es on the stack though.
精彩评论