www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How do I refer to /this/ object?

reply Nils Hensel <nils.hensel web.de> writes:
Hello everybody,

what is D's equivalent to C++'s "this" keyword? How can I refer to the 
object instance itself?

Nils
Apr 23 2005
next sibling parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
Umm... this?  How does the D "this" differ from the C++ "this"?

-[Unknown]


 Hello everybody,
 
 what is D's equivalent to C++'s "this" keyword? How can I refer to the 
 object instance itself?
 
 Nils
Apr 23 2005
prev sibling next sibling parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
According to http://www.digitalmars.com/d/lex.html#keyword :)
D has also 'this' keyword.

this.param = 123;

Andrew. 
Apr 23 2005
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Sun, 24 Apr 2005 00:24:30 +0200, Nils Hensel wrote:

 Hello everybody,
 
 what is D's equivalent to C++'s "this" keyword? How can I refer to the 
 object instance itself?
 
The answer is 'this'. Example code: //---------------- import std.stdio; class Foo { char[] v; this() { v = "abc".dup; } void disp(char[] v) { writefln("%s", this.v ~ v); } } void main() { Foo f = new Foo; f.disp("def"); } //------------ -- Derek Parnell Melbourne, Australia 24/04/2005 9:05:35 AM
Apr 23 2005
parent reply Nils Hensel <nils.hensel web.de> writes:
Derek Parnell schrieb:
 The answer is 'this'. 
Thanks to all for your answers. Actually I tried this but always got a compiler error which I thought was connected to "this". What actually got me was: <D Documentation> If there is no constructor for a class, but there is a constructor for the base class, a default constructor of the form: this() { } is implicitly generated. </D Documentation> Somehow I was thinking that the constructor would be inherited. I'm too tired for programming right now I believe. Too many stupid ideas. Better get some sleep. ;-) Thanks anyway, Nils
Apr 23 2005
parent "TechnoZeus" <TechnoZeus PeoplePC.com> writes:
"Nils Hensel" <nils.hensel web.de> wrote in message
news:d4emra$2ksa$1 digitaldaemon.com...
 Derek Parnell schrieb:
 The answer is 'this'.
Thanks to all for your answers. Actually I tried this but always got a compiler error which I thought was connected to "this". What actually got me was: <D Documentation> If there is no constructor for a class, but there is a constructor for the base class, a default constructor of the form: this() { } is implicitly generated. </D Documentation> Somehow I was thinking that the constructor would be inherited. I'm too tired for programming right now I believe. Too many stupid ideas. Better get some sleep. ;-) Thanks anyway, Nils
It is inhereted, but under the name "super" rather than "this" so that you have access to both. TZ
Apr 24 2005