Weird (?) problem

James Dunne james.jdunne at
Thu Aug 10 05:47:26 PDT 2006

mike wrote:
> [snip]
> So: In the TypeType static c'tor, I make a new instance of that class.  
> Then the normal c'tor is called, trying to setup itself in the tree  
> system, telling everybody that it is indead a type. So ... it needs the  
> very instance of the TypeType class that is being constructed at that 
> very  moment ... so: access violation.
> ' class TypeType : TypeBase
> ' {
> '     TypeBase instance = null;
> '    static this() { instance = new TypeType(); }
> '     static TypeBase opCall() { return instance; }
> '     this()
> '     {
> '          addObject("typetype");
> '          getObject("typetype").setType(TypeType()); // crashes: 
> access  violation
> '     }
> ' }
> It's a bit of a chicken and egg problem. I can't new an instance of 
> that  class unless there's already one instance there. Is it possible to 
> solve  that somehow? Something like an "after_this()" or the like? I 
> guess I've  outsmarted myself there and I have no idea what to do about 
> it besides  reverting to the old system with that stupid, ugly construct 
> function  (shudder).
> Any ideas would be really appreciated.
> -Mike

Create two constructors for your TypeType classes.

One private c'tor with a dummy parameter to differentiate it from the 
normal public c'tor.

Call that private c'tor in your static c'tor; the private c'tor should 
obviously _not_ call into your registration system.

The normal public c'tor should call the registration system, just like 
in your code above.

class TypeType : TypeBase {
     TypeBase instance = null;

     static this() {
         instance = new TypeType(true);
     static TypeBase opCall() { return instance; }

     public this() {

     private this(bool dummy) {
         // Just a dummy to suppress infinite recursion in constructor
         // calls...

I'm curious as to the purpose of the multiple registration of the 
"typetype" named instance in your registry system.  I hope you get the 
basic idea of what I'm proposing so you can fix these issues yourself. :)

Version: 3.1
GCS/MU/S d-pu s:+ a-->? C++++$ UL+++ P--- L+++ !E W-- N++ o? K? w--- O 
M--@ V? PS PE Y+ PGP- t+ 5 X+ !R tv-->!tv b- DI++(+) D++ G e++>e 
h>--->++ r+++ y+++

James Dunne

More information about the Digitalmars-d-learn mailing list