We’re rewarding the question askers & reputations are being recalculated! Read more.
2 edited title
| link

using this anin a constructor, why does it work?

1
source | link

using this an a constructor, why does it work?

I got two classes.

  • busiObj(created from ancient flat files)
  • key(provides means to tell busiObjs apart)

key has a c'tor that takes a busiObj

public key(busiObj foo)
{
    bar = foo.someThing;
    [...]
}

I want to create the key in the c'tor of the busiObj

public busiObj(...)
{
    myKey = new key(this);
    [...]
}

However: Why on earth does it work?
I firmly expected the compiler to throw something like:
"You cannot reference an object that is currently being made, duh".

So: How is it that I can reference an object while it is still being constructed?