Referencing the object that a construtor is currently constructing/working on.

In the code below, I would pass the name of the object which is being constructed through the n parameter in order to create a record in the currentDialogue dictionary which would pair that object’s ID with its actual name. Assuming that the object has already been created and is referenceable when the constructor works on it.

      class Node{
	function Node (i : String, n : Node){
		id = i;
		currentDialogue *= n;*
  • }*
  • var id : String;*
    }
    If I wanted to construct an object:
    n1 = Node (“Why have you returned?”,n1);
    My question is twofold:
    1. Is it possible to use a reference to the object which is currently being constructed in the constructor for that object’s class?
    2. If it is possible, Is there a better method than what is used in my sample code?

You can’t pass a reference to the created object because it’s returned be the constructor. So outside of the constructor the object available after the constructor has finished.

But you can use this:

class Node{
var id : String;

function Node (i : String){
    id = i;
    currentDialogue *= this;*

}
}
Inside of all non static functions of a class, this references the current instance. It’s one of the most important things in OOP :wink: