| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
November 28, 2011 References | ||||
|---|---|---|---|---|
| ||||
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)?
| ||||
November 28, 2011 Re: References | ||||
|---|---|---|---|---|
| ||||
Posted in reply to David Currie | 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
| |||
November 28, 2011 Re: References | ||||
|---|---|---|---|---|
| ||||
Posted in reply to David Currie | 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..
}
| |||
November 28, 2011 Re: References | ||||
|---|---|---|---|---|
| ||||
Damn you Ali!! xD | ||||
November 28, 2011 Re: References | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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
| |||
November 28, 2011 Re: References | ||||
|---|---|---|---|---|
| ||||
Posted in reply to David Currie | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply