[cpp] [list][destruktor] problem z destruktorem w liscie wskaznikow wlasnych obiektow
Ostatnio zmodyfikowano 2021-02-13 15:38
aaadam Temat założony przez niniejszego użytkownika |
[cpp] [list][destruktor] problem z destruktorem w liscie wskaznikow wlasnych obiektow » 2021-02-13 14:16:40 hej, czemu destruktory sie nie uruchomiają ? #include <iostream> #include <list>
using namespace std;
class Base { public: Base() { cout << "tworze base" << endl; } void say() { cout << "say base" << endl; } virtual ~Base() { cout << "delete base" << endl; } };
class A : public Base { public: A() { cout << "tworze A" << endl; } void say() { cout << "say A" << endl; } ~A() { cout << "delete A" << endl; } };
class B : public Base { public: B() { cout << "tworze B" << endl; } void say() { cout << "say B" << endl; } ~B() { cout << "delete B" << endl; } };
class C : public Base { public: C() { cout << "tworze C" << endl; } void say() { cout << "say C" << endl; } ~C() { cout << "delete C" << endl; } };
int main() { list < Base * > my_list; list < Base * >::iterator it; my_list.push_back( new Base() ); my_list.push_back( new A() ); my_list.push_back( new B() ); my_list.push_back( new C() ); cout << endl << "rozmiar listy " << my_list.size() << endl; my_list.pop_back(); my_list.pop_back(); my_list.pop_back(); my_list.pop_back(); cout << endl << "rozmiar listy " << my_list.size() << endl; return 0; } [ cpp / ] |
|
pekfos |
» 2021-02-13 15:01:21 Bo nie wywołujesz delete. |
|
aaadam Temat założony przez niniejszego użytkownika |
» 2021-02-13 15:08:30 myślałem że to : my_list.pop_back(); uruchamia delete na ostatnim elemencie listy ... to jak to powinno byc? |
|
pekfos |
» 2021-02-13 15:15:52 Lista nie ma wiedzy czy i jak należy zwolnić pamięć, więc tego nie robi. Musisz to zrobić sam, albo użyj innego typu elementu, np std::unique_ptr<>. |
|
aaadam Temat założony przez niniejszego użytkownika |
» 2021-02-13 15:19:59 no to lipa, fajnie by było jak by destruktory były odpalane automatycznie(ablo dało się to jakoś skonfigurować), własnie znalazłem artykuł o tych uniqueptr, dzięki |
|
aaadam Temat założony przez niniejszego użytkownika |
» 2021-02-13 15:34:01 Ogarnąłem dzięki unique_ptr kod poniżej dla potomnych :) #include <iostream> #include <list> #include <memory>
using namespace std;
class Base { public: int x; Base() { x = 0; cout << "create base" << endl; } virtual void say() { cout << "say base" << endl; } virtual ~Base() { cout << "delete base" << endl; } };
class A : public Base { public: A() { x = 1; cout << "create A" << endl; } void say() { cout << "say A" << endl; } ~A() { cout << "delete A" << endl; } };
class B : public Base { public: B() { x = 2; cout << "create B" << endl; } void say() { cout << "say B" << endl; } ~B() { cout << "delete B" << endl; } };
class C : public Base { public: C() { x = 3; cout << "create C" << endl; } void say() { cout << "say C" << endl; } ~C() { cout << "delete C" << endl; } };
int main() { A * a = new A(); B * b = new B(); C * c = new C(); cout << "usuwanie-------------------------------------------" << endl; delete a; delete b; delete c; cout << "listy----------------------------------------------" << endl; list < unique_ptr < Base >> my_list; my_list.push_back( unique_ptr < Base >( new Base() ) ); my_list.push_back( unique_ptr < Base >( new A() ) ); my_list.push_back( unique_ptr < Base >( new B() ) ); my_list.push_back( unique_ptr < Base >( new C() ) ); cout << endl << "rozmiar listy " << my_list.size() << endl; for( auto const & i: my_list ) { i->say(); } my_list.pop_back(); my_list.pop_back(); my_list.pop_back(); my_list.pop_back(); cout << endl << "rozmiar listy po usuwaniu " << my_list.size() << endl; return 0; }
|
|
aaadam Temat założony przez niniejszego użytkownika |
» 2021-02-13 15:38:42 może jeszcze 1 pytanie, dodałem pytanie w logu dlaczego 2 razy sie podpala dla klasy bazowej nie powinno raz ? [Edit] Wszystko ok, źle patrzyłem w logi, nie mogę usunąć ostatniej wiadomości. Jak admin może to można usunąć
g++ main.cpp && ./a.out create base create A create base create B create base create C usuwanie------------------------------------------- delete A delete base delete B delete base delete C delete base listy---------------------------------------------- create base <--- czemu 2 razy ?? create base create A create base create B create base create C
rozmiar listy 4 say base say A say B say C delete C delete base delete B delete base delete A delete base <--- czemu 2 razy ?? delete base
rozmiar listy po usuwaniu 0
|
|
« 1 » |