delegating constructors and "this = ..."
jpf
spam at example.com
Mon Feb 8 08:00:45 PST 2010
Hi,
I've come across some strange behavior using delegating constructors and
assignment to this. Please have a look a this testcase:
--------------------------------------
class Test
{
static Test[void*] lookupTable;
public int i = 0;
public this (void* ptr)
{
//Check if there already is a D object for this ptr
if(ptr in lookupTable)
{
this = lookupTable[ptr];
assert(this.i == 15); /* ok */
return;
}
i = 15;
lookupTable[ptr] = this;
}
public this (void* ptr, bool fake)
{
this(ptr);
assert(this.i == 15); /* fails */
}
}
void main()
{
int a,b; /*for different pointers*/
Test test1 = new Test(&a);
Test test2 = new Test(&a);
Test test3 = new Test(&b, false);
Test test4 = new Test(&b, false);
assert(test1 is test2); /* ok */
assert(test2.i == 15); /* ok */
assert(test3 is test4); /* fails */
assert(test4.i == 15); /* fails */
}
--------------------------------------
Tested with dmd 1.055. As soon as the constructors are delegated the
code doesn't work anymore. The assignment "this = lookupTable[ptr];"
still succeeds but in the "this(void* ptr, bool fake)" constructor the
this reference seems wrong. The returned object also isn't the one
assigned to "this".
I'm not sure if this is a bug, but it's at least unexpected behavior.
Should I file a bug for this issue? Would the correct bug-tracker for
this be http://d.puremagic.com/issues ?
More information about the Digitalmars-d
mailing list