Re: Reference Types and Value Types

From: Lateralus [MCAD] (dnorm252_at_yahoo.com)
Date: 09/03/04


Date: Thu, 2 Sep 2004 20:55:48 -0500

daniel,
    That is correct. There is only one storage location of the object. The
variables all just point to the same object.

-- 
Lateralus [MCAD]
"daniel" <user@example.com> wrote in message 
news:OfNECeTkEHA.3724@TK2MSFTNGP11.phx.gbl...
> Ahh, thank you Lateralus for the quick reply. I have a quick follow up 
> question, though:
>
> If a reference type is always passed by reference, then (while stupid and 
> messy):
>
> <code>
> // with a return type this time!
> void func1()
> {
> SomeType myType = new myType();
> func2(myType);
> }
> void func2(SomeType myType)
> {
> func3(mytype);
> }
> void func3(SomeType myType)
> {
> Console.Write(myType.ToString());
> }
> </code>
>
> will not cause a substantial amount of increased overhead at all (other 
> than variables on the stack), because only one object is every created.
>
>
> Thanks,
> -daniel
>
>
>
> Lateralus [MCAD] wrote:
>
>> daniel,
>>     To my knowledge you can't pass a "reference" type by value if you 
>> wanted to. It is automatically passed by reference. If you have a value 
>> type and you want to maintain it's value across method calls, you need to 
>> pass it by reference.
>>
>> example should print out 12:
>>
>> private void Test()
>> {
>>     int index = 0;
>>     Test2(ref index);
>>     Console.Write(index.ToString());
>> }
>>
>> private void Test2(ref index)
>> {
>>     index = 12;
>> }
>> 


Relevant Pages

  • Re: back once again...
    ... reference to type ... a, signed char ... int dycObjectP(dyt obj); ... void dycBeginClass; ...
    (comp.lang.misc)
  • [PATCH 12/15] dm: add exports
    ... Export core device-mapper functions for manipulating mapped devices ... void dm_get ... struct dm_target; ... * Drop the reference with dm_put when you finish with the object. ...
    (Linux-Kernel)
  • Re: Whats your favorite language and why? (LINUX)
    ... efficient distributed intra-process message-passing algorithm on several ... architectures that incurs virtually zero-overheads. ... by reference without having to do anything at all. ... void producer_threads{ ...
    (comp.programming)
  • Re: C to Java migration - how to cope with passing by value only?
    ... > void Foo(Color cl, JButton bn) { ... In C++, with reference parameters, I suspect you're not really doing quite ... public JButton button; ...
    (comp.lang.java.programmer)
  • Re: pass by Reference/value ???
    ... > void foo ... this would be called "passing by reference". ... foo receives a local copy of s and any changes that it makes to s only ... Since s is a pointer, a variable storing the address of s is a pointer to ...
    (comp.lang.cpp)