'The function clears its own values [duplicate]
Can someone explain to me why, in the Push_Back function, the values of a structure are erased?
class Lista2
{
public:
typedef struct Element
{
Element(int newValue)
{
value = newValue;
next = nullptr;
}
Element* next;
int value;
Element* prev;
};
Element* first;
void init() {
Element ok(0);
first = &ok;
}
void Push_Back(int value)
{
Element* temp = first;
Element* temp2;
temp2 = new Element(value);
if (temp != nullptr)
{
while (temp->next != nullptr)
{
temp = temp->next;
}
temp->next = temp2;
}
else first = temp2;
}
I called the function: init() and after Push_Back(). In Push_back I see this in debuger: 
Can someone explain to me why, in the Push_Back function, the values of a structure are erased?
Solution 1:[1]
In init, you created variable on stack, took it address and saved it as the structure member.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PsychoX |
