Which class D "out" is useful for storage?

I know that outworks almost like refexcept that it initializes the passed argument to its default value when entering the function.

My question is, in what situation would it be useful to "reset a variable when entering a function"?

It would be great if someone could show me an example where outit would be more useful than ref.

+4
source share
3 answers

The main difference between refand outis intent. When used ref, it is expected that the value to be passed will be used by the function. And the function may or may not install it. You can even have const refit if you want, which would mean that the variable that was passed in will not be copied, but it also cannot be changed.

const out, , , out , ​​ . out - , , . ref , ref , , , out , .

, out , , . out , , . out , , , , , , , .

, out , , , , , out ( ). , out, (, "" ).

+7

out ref, , . , init , out, .

, , , :

void test(out int n)
{
    //Do nothing
}

void main()
{
    int x;
    test(x);
}

C, C, , .

+4

, "out" , - . "ref", . , , . Ref, , ++, , .

So, let's say if you have some kind of object for a function that has not been initialized, you can pass it using "out" and accept the "out" function, and you can create this object in the function. However, if the function accepts "ref" and you pass in an uninitialized object, I think the compiler will give you an error.

Charles Petzold The Zero e-book (available for free download) explains all these delicacies in detail. I suggest you read it.

+2
source

Source: https://habr.com/ru/post/1547790/


All Articles