im not sure if i understand correctly how this class works. its an example of stack.
ref class Stack
{
private:
ref struct Item // Defines items to store in the stack
{
Object^ Obj; // Handle for the object in this item
Item^ Next; // Handle for next item in the stack or nullptr
Item(Object^ obj, Item^ next): Obj(obj), Next(next){} // Constructor
};
Item^ Top; // Handle for item that is at the top
public:
void Push(Object^ obj) // Push an object onto the stack
{
Top = gcnew Item(obj, Top); // Create new item and make it the top
}
Object^ Pop() // Pop an object off the stack
{
if(Top == nullptr) // If the stack is empty
return nullptr; // return nullptr
Object^ obj = Top->Obj; // Get object from item
Top = Top->Next; // Make next item the top
return obj;
}
};
i cant figure out how exactly Push function works. in the class definition its Top=gcnew Item(obj, Top)so basically it says that Top equals Next. So how does the Stack class determine the Next item if its always the one on the top of the stack?
I think you misinterpret the line:
It means:
Nextof the new Item to what was previously the Topso by calling
Top->Nextyou get to the previous item (the one “underneath”Top)