class Name_value
{
public:
int value;
string name;
Name_value(string a, int b)
: name(a), value(b)
{
}
~Name_value()
{
cout << "Object destroyed" << '\n';
}
};
vector<Name_value> v;
int main()
{
int score;
string name;
cout << "Enter a name and value." << '\n';
while (cin >> name >> score)
{
Name_value thedata(name, score);
v.push_back(thedata);
}
for (Name_value x : v)
{
cout << x.name << '\n';
}
}
Output:
Enter a name and value.
c 18
Object destroyed
d 15
Object destroyed
Object destroyed
a 14
Object destroyed
Object destroyed
Object destroyed
b 21
Object destroyed
Object destroyed
Object destroyed
Object destroyed
So my question is what exactly is happening here? Say I type c 18 I create an object put it into the vector and then that object gets destroyed, then I type d 15 which again creates a new object puts it into the vector and destroys it. Why am I getting more destroyed objects everytime?