Thread overview
How do I refer to /this/ object?
Apr 23, 2005
Nils Hensel
Apr 23, 2005
Andrew Fedoniouk
Apr 23, 2005
Derek Parnell
Apr 23, 2005
Nils Hensel
Apr 24, 2005
TechnoZeus
April 23, 2005
Hello everybody,

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

Nils
April 23, 2005
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
April 23, 2005
According to http://www.digitalmars.com/d/lex.html#keyword :) D has also 'this' keyword.

this.param = 123;

Andrew.


April 23, 2005
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
April 23, 2005
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
April 24, 2005
"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