1
General / Re: Abstract class for several screens, trouble with destructors
« on: June 19, 2019, 12:48:36 pm »
Hi.
Thanks for the fast reply.
Yeah, you're right. I don't know why, but for some reason i thought it had to do with SMFL.
You were also right in your second paragraph, it did not work without the destructor either actually. And your third paragraph is great advice for posting a problem or just debugging on your own, because when i was cutting all excess code to get to the core of the problem i found the reason:
This would be okay i guess if the objects they where pointing to were on the heap, but, these vectors actually just point to class members on the stack, they go out of scope when deleting the object either way, so it's just foolish to try to delete them in the destructor of course.
Thanks a lot for your help!
PS:
About using smart pointers, i'm about to get into them, i read everywhere that in modern c++, youre "never" supposed to use the word new or delete. But the thing is that i'm just out of my first class of c++ in uni, and there we where taught to do it this way. = p
Thanks for the fast reply.
Yeah, you're right. I don't know why, but for some reason i thought it had to do with SMFL.
You were also right in your second paragraph, it did not work without the destructor either actually. And your third paragraph is great advice for posting a problem or just debugging on your own, because when i was cutting all excess code to get to the core of the problem i found the reason:
Screen_1::~Screen_1()
{
for (auto i : texts) delete i;
for (auto i : shapes) delete i;
}
{
for (auto i : texts) delete i;
for (auto i : shapes) delete i;
}
This would be okay i guess if the objects they where pointing to were on the heap, but, these vectors actually just point to class members on the stack, they go out of scope when deleting the object either way, so it's just foolish to try to delete them in the destructor of course.
Thanks a lot for your help!
PS:
About using smart pointers, i'm about to get into them, i read everywhere that in modern c++, youre "never" supposed to use the word new or delete. But the thing is that i'm just out of my first class of c++ in uni, and there we where taught to do it this way. = p