I have the following class:
public class STElement
{
public int _value;
public STElement _next;
public int _index;
public STElement()
{
_value = 0;
_next = null;
_index = 0;
}
}
In the runtime of the programm I want to create some objects like this:
_rootStack1 = new STElement();
_rootStack2 = new STElement();
_rootStack3 = new STElement();
But I only want _rootStack1 to have the _index = 0;
So how can I do it, that only one of the three objects get _index?
You can create a constructor that takes the value of an index and assigns it to the variable.
This will not enforce that the object is the only one that has such a
0index – this is not something an object can do directly.Note on style – it is not good practice to have
publicfields. You should use properties to expose them.