Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <memory>
- #include <iostream>
- using namespace std;
- struct BadPlayer
- {
- shared_ptr<BadPlayer> drugi_igrac;
- //Zbog toga sto ivan i marko pokazuju jedan na drugog, shared_ptr nikad nece moci da unisti niti jedan od ta dva
- //objekta te ovaj destruktor nece nikada biti pozvan.
- ~BadPlayer() { cout << "Unistavam igraca sa deljenom referencom\n"; }
- };
- struct GoodPlayer{
- weak_ptr<GoodPlayer> drugi_igrac;
- //Zato sto imamo slabu referencu na drugog igraca, ovaj destruktor ce se pozvati na kraju scopea u kom su jovan i milan deklarisani
- ~GoodPlayer() { cout << "Unistavam igraca sa slabom referencom \n"; }
- };
- int main()
- {
- shared_ptr<BadPlayer> ivan(new BadPlayer());
- shared_ptr<BadPlayer> marko(new BadPlayer());
- ivan->drugi_igrac = marko;
- marko->drugi_igrac = ivan;
- shared_ptr<GoodPlayer> jovana(new GoodPlayer());
- shared_ptr<GoodPlayer> milana(new GoodPlayer());
- jovana->drugi_igrac = milana;
- milana->drugi_igrac = jovana;
- }
Add Comment
Please, Sign In to add comment