www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - References

reply David Currie <curriedr iinet.net.au> writes:
I am a newbie to D. From (C++,Java,others...) background.

In C++ I can say

void f1(int& pInt)
{
   pInt = 1;
}

which sets pInt(which is outside f1)
because although pInt (at compile time) is a Value
in reality it is passed by reference(address).

Now

void f2(int* pIntPtr)
{
   *pIntPtr = 1;
   ++pIntPtr;
   *pInt   = 2;
}
sets (the contents of) pInt to 1 (and the next immediate address to 2)

All this is of course standard C++.

How is this type of thing done in D
(changing objects by passing by reference etc)?
Nov 28 2011
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/28/2011 05:41 PM, David Currie wrote:
 I am a newbie to D. From (C++,Java,others...) background.

 In C++ I can say

 void f1(int& pInt)
 {
 pInt = 1;
 }

 which sets pInt(which is outside f1)
 because although pInt (at compile time) is a Value
 in reality it is passed by reference(address).

 Now

 void f2(int* pIntPtr)
 {
 *pIntPtr = 1;
 ++pIntPtr;
 *pInt = 2;
 }
 sets (the contents of) pInt to 1 (and the next immediate address to 2)

 All this is of course standard C++.

 How is this type of thing done in D
 (changing objects by passing by reference etc)?
Pointers are the same in D but needed far less than C++. For parameter passing, the ref keyword can be used: void f3(ref int pInt) { // ... } Also check out 'out' parameters: void f4(out int pInt) { // ... } The difference from ref is the fact that out parameters are initialized to .init of their type when entering the function. They are documented here: http://d-programming-language.org/function.html Additionally, you may find it surprising that classes are reference types in D (unlike structs, which are value types as in C and C++). So you don't need to use the ref keyword, as the class object would be passed by reference as the class variable: class C { // ... } void f5(C c) // <-- reference to the class object { // ... } Other reference types of D are dynamic arrays and associative arrays (importantly, fixed-length arrays are value types!) Ali
Nov 27 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Use ref:

void f1(ref int val)
{
    val = 1;
}

Another one is 'out', which initializes the type with it's .init value
on function entry:

void foo(out int val) {}

This is (I believe) equivalent to the following:

void foo(ref int val)
{
    val = int.init;
    // your code here..
}
Nov 27 2011
prev sibling next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Damn you Ali!! xD
Nov 27 2011
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/27/2011 11:15 PM, Andrej Mitrovic wrote:
 Damn you Ali!! xD
No no: Both of us are waiting for someone to correct us. :) Ali
Nov 27 2011
prev sibling parent Graham Fawcett <fawcett uwindsor.ca> writes:
On Mon, 28 Nov 2011 17:41:08 -0800, David Currie wrote:

 I am a newbie to D. From (C++,Java,others...) background.
 
 In C++ I can say
 
 void f1(int& pInt)
 {
    pInt = 1;
 }
 
 which sets pInt(which is outside f1)
 because although pInt (at compile time) is a Value in reality it is
 passed by reference(address).
 
 Now
 
 void f2(int* pIntPtr)
 {
    *pIntPtr = 1;
    ++pIntPtr;
    *pInt   = 2;
 }
 sets (the contents of) pInt to 1 (and the next immediate address to 2)
 
 All this is of course standard C++.
 
 How is this type of thing done in D
 (changing objects by passing by reference etc)?
Try void f1(ref int pInt) Graham
Nov 28 2011