digitalmars.D.learn - References
- David Currie (20/20) Nov 28 2011 I am a newbie to D. From (C++,Java,others...) background.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (30/50) Nov 27 2011 Pointers are the same in D but needed far less than C++.
- Andrej Mitrovic (14/14) Nov 27 2011 Use ref:
- Andrej Mitrovic (1/1) Nov 27 2011 Damn you Ali!! xD
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/4) Nov 27 2011 No no: Both of us are waiting for someone to correct us. :)
- Graham Fawcett (4/31) Nov 28 2011 Try
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
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
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
On 11/27/2011 11:15 PM, Andrej Mitrovic wrote:Damn you Ali!! xDNo no: Both of us are waiting for someone to correct us. :) Ali
Nov 27 2011
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