Copying structures

Structures of the same type can be copied entirely into each other using the '=' assignment operator. Let's demonstrate this rule using an example of the structure Result. We get the first instance of r from the calculate function.

void OnStart()
{
   ...
   Result r = calculate(s);
   r.print();
   // will output to the log:
   // 0.5 1 ok
   // 1.00000 2.00000 3.00000
   ...
   Result r2;
   r2 = r;
   r2.print();
   // will output to the log the same values:
   // 0.5 1 ok
   // 1.00000 2.00000 3.00000
}

Then, the variable Result r2 was additionally created, and the contents of the r variable, all fields concurrently, were duplicated into it. The accuracy of the operation can be verified by outputting to the log using the method print (the lines are given in the comments).

It should be noted that defining two types of structures with the same set of fields does not make the two types the same. It is not possible to assign a structure to another one completely, only memberwise assignment is permitted in such cases.

A little later, we'll talk about structure inheritance, which will give you more options for copying. The fact is that copying works not only between structures of the same type but also between related types. However, there are important nuances, which we will cover in the Layout and inheritance of structures section.