this reference in c'tor

Mike vertex at gmx.at
Sun Nov 25 12:15:57 PST 2007


Hi!

I know that there has to be a reason why one can't use "this" references in the constructor. But why? I can assign members. So "this" exists in the constructor ... why does the (simplified) example below trigger the assert?

' class Foo
' {
'    public this(Foo parent)
'    {
'         assert(parent !is null);
'         children ~= new(Foo(this));
'    }
'    private Foo[] children;
' }

(Disclaimer: I know that this expamle is an infinite loop)

I need to build a tree and certain Foos need to build default childs when they're instantiated. Having a separate construct method ... no, that's just not it. The Foo c'tor should build a complete Foo instance, not half of it.

-Mike (frustrated)


More information about the Digitalmars-d-learn mailing list