开发者

Copy constructor and assignment operator in CLI

开发者 https://www.devze.com 2023-03-07 16:30 出处:网络
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 somethi

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 classes, 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 classes on the stack though.

0

精彩评论

暂无评论...
验证码 换一张
取 消