Why doesn't the .NET CLI provide synthesized copy constructors and assignment operators for referenced classes?

I am writing a simple GUI using Visual C ++ in the .NET framework and I am curious why there are no synthesized copy constructors or assignment operators for referenced classes?

Basically, I started a Windows Form application in Visual Studio and had my own settings data structure. But I needed instances (and pointers to instances) in my MainForm class, and since the latter is a referenced class, I needed to create a struct reference too.

+3


source to share


1 answer


So long and short due to the fact that C ++ CLI is a .NET language, which is a reference language (garbage collector, but in this case they are indistinguishable). Almost everything is referenced by default, which means that all instances are accessible via descriptors, so in

Foo^ a = gcnew Foo(); Foo^ b = a;

      

a

and b

point to the same object. The same is true for other knowledge-oriented languages ​​like Java.



A side effect of this is, unlike C ++, no one "owns" the instance it points to a

. The actual lifetime of a is simply longer than all handles. This makes it impossible to create a copy by default. Let's take a simple reference class:

ref class Foo
{
   Bar^ bar;
};

      

When we make a copy of Foo, the compiler doesn't know if Foo owns the bar or not. Thus, it cannot decide whether to copy the descriptor (shallow copy) or create a new one Bar

(deep instance). Thus, the developers of such a language usually do not provide a default copy constructor.

+1


source







All Articles