www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Question about using class refs as AA keys

reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
Is it safe to do something like this?

Class Node
{
protected:
    static Node[Node] nodes;
public:
    this()
    {
        nodes[this]=this;
    }
    ~this()
    {
        delete nodes[this];
    }
}

I don't know how the GC works, but is this safe?  I'm just wondering if the
class references will change, causing the key to become invalid when the GC
is run.  Or are the class refs doubly indirected, making it safe?
Dec 30 2004
parent reply "Walter" <newshound digitalmars.com> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message
news:cr19ka$q75$1 digitaldaemon.com...
 Is it safe to do something like this?

 Class Node
 {
 protected:
     static Node[Node] nodes;
 public:
     this()
     {
         nodes[this]=this;
     }
     ~this()
     {
         delete nodes[this];
     }
 }

 I don't know how the GC works, but is this safe?  I'm just wondering if

 class references will change, causing the key to become invalid when the

 is run.  Or are the class refs doubly indirected, making it safe?

Yes, it's safe to do it!
Dec 30 2004
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
 Yes, it's safe to do it!

Sweet :)
Dec 30 2004