Re: please help me understand ref and out

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Bruce Wood (brucewood_at_canada.com)
Date: 12/08/04


Date: 8 Dec 2004 12:36:54 -0800


"out" is a more specific form of "ref". By giving the compiler more
information you allow it to make more checks for you. By designating a
parameter as "out" you are telling the compiler, "I guarantee that when
this method returns, this argument will have been set to a value by the
method."

The compiler then does the necessary static checking to ensure that
your method is living up to its contract.

"ref" is more vague: it states that the method may or may not choose to
assign a (new) value to the variable.

You should use "out" whenever your design calls for multiple return
values from a method, because you then get "free" code correctness
checking. It is much less often that you need "ref". If you just use
"ref" all over the place the compiler won't care, but then you give up
the opportunity to find bugs at compile time rather than at run time.



Relevant Pages

  • Smart pointer dumping core and specialization question
    ... I'm working around with smart pointers mostly as a learning exercise: ... I have a single-rooted hierarchy with the root class Object. ... Is there any reason why the compiler just doesn't do the obvious ... Ref(const Ref& ref): ptr) { ...
    (comp.lang.cpp)
  • Re: ref vs. out: same at runtime, same/different at compiler time?
    ... This is what I'm referring to and this is what I ... If I'm writing code that calls the method, the compiler would not be able to ... tell the difference between the ref and the out method, ... static void Foo ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Incorrect "variable might not have been initialized"
    ... String ref; ... It's clearly Boolean;-) And true. ... are formally constant. ... (or if the compiler is incorrect then so is the JLS in this matter). ...
    (comp.lang.java.programmer)
  • Re: reading a declaration
    ... > argument by pointer with its address, or by reference. ... What I mean is: #define ref x ... does EXCEPT with define there is no scope or type checking. ... couldn't a compiler implement the references by precompiling them ...
    (comp.lang.cpp)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... attributes helps both the writer and the compiler. ... > catch at compile time any out of boundaries access. ... C++ array like types don't have these properties. ... Maybe, if one gets used to template programming, the basis of the ...
    (comp.lang.ada)