Answer the question
In order to leave comments, you need to log in
Why does unordered_map stop working after calling the destructor?
I have this class:
class accauntant: public position
{
private:
unordered_map<string, float> stakes;
public:
~accauntant() {stakes.clear(); name.clear(); surname.clear(); patronymic.clear(); salary = 0;}
float getStake(position employee)
{
if(stakes.find(employee.getName() + employee.getSurname() + employee.getPatronymic()) != stakes.end())
return stakes[employee.getName() + employee.getSurname() + employee.getPatronymic()];
else
return 0;
}
void setStake(position employee, float f)
{
stakes[employee.getName() + employee.getSurname() + employee.getPatronymic()] = f;
}
void addEmp(position employee)
{
stakes[employee.getName() + employee.getSurname() + employee.getPatronymic()] = 1;
}
float calculateSalary(position employee)
{
return stakes[employee.getName() + employee.getSurname() + employee.getPatronymic()] * employee.getSalary();
}
};
class position
{
protected:
string name, surname, patronymic;
int salary;
public:
position(){salary = 0;}
position(string n, string s, string p, int sal)
{
name = n;
surname = s;
patronymic = p;
salary = sal;
}
string getName() const
{
return name;
}
string getSurname() const
{
return surname;
}
string getPatronymic() const
{
return patronymic;
}
int getSalary() const
{
return salary;
}
void setName(string n)
{
name = n;
}
void setSurname(string s)
{
surname = s;
}
void setPatronymic(string p)
{
patronymic = p;
}
void setSalary(int s)
{
salary = s;
}
};
Answer the question
In order to leave comments, you need to log in
As in a joke (obviously from the 70s, when there was a Football war between Honduras and El Salvador).
“Something worries me about Honduras.
- So don't scratch it.
Calling the destructor will cascade the destructors of all fields, and this will lead to the fact that in place of u_m there will be garbage like nullptr and dangling pointers.
Autodestructors are the most important feature of C++, which distinguishes it from other languages, and an explicit destructor call is rarely needed when we want a little more manual memory management. Here is an example from our project. There is a allocated piece of memory, and we want to destroy the object there and create a new one in its place, without giving back-allocating memory. We assume that the objects are the same, otherwise all the magic will be lost.
wiTile->~WiTile();
new (wiTile) WiTile(client(), icons, clazz, tileSettings(i));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question