简体   繁体   中英

how to return a list of objects without destroying them

How to fix the function 'func' so that it returns the objects without being destroyed?

function 'func' must add the objects to a list and return them but be destroyed

The Smoothy abstract class has a purely virtual description method (). DecoratorSmoothy contains a smoothy, description () and getPret () methods return the description and price aggregate smoothy. SmoothyCuFream and SmoothyCuUmbreluta classes add the text “cu crema” respectively “cu umbreluta” in the description of the smoothy contained. The price of a smoothy that has the cream increases by 2 euro, the one with the umbrella costs an extra 3 euro. BasicSmoothy class is a smoothy without cream and without umbrella, method description () returns the name of the smothy

#include <iostream>
#include <vector>
using namespace std;

class Smoothy {
private:
    int pret=0;
public:
    virtual string descriere() = 0;
    int getPret(){
        return pret;
    }
    void setPret(int a) {
        pret += a;
    }
};
class BasicSmooty : public Smoothy {
private:
    string nume;
public:
    BasicSmooty(string n) :
        nume { n } {}
    string descriere() {
        return nume;
    }
};

class DecoratorSmoothy : public Smoothy {
private:
    Smoothy* smooty;
public:
    DecoratorSmoothy() = default;
    DecoratorSmoothy(Smoothy* n) :
        smooty{ n } {}
    string descriere() {
        return smooty->descriere();
    }
    int getPret() {
        return  smooty->getPret();
    }
};
class SmootyCuFrisca : public DecoratorSmoothy {

private:
    BasicSmooty bsc;
public:
    SmootyCuFrisca(string desc) :
        bsc{ desc } {}
    string descriere() {
        setPret(2);
        return bsc.descriere() + " cu frisca ";
    }
};

class SmootyCuUmbreluta : public DecoratorSmoothy{

private:
    BasicSmooty bsc;
public:
    SmootyCuUmbreluta(string desc) :
        bsc{ desc } {}
    string descriere() {
        setPret(3);
        return bsc.descriere() + " cu umbreluta ";
    }
    ~SmootyCuUmbreluta() {
        cout << "rip";
    }
};
vector<Smoothy*> func(void)
{
    std::vector<Smoothy*> l;

    SmootyCuFrisca a1{ "smooty de kivi" };
    SmootyCuUmbreluta a2{ "smooty de kivi" };
    SmootyCuFrisca a3{ "smooty de capsuni" };
    BasicSmooty a4{ "smooty simplu de kivi" };

    l.push_back(&a1);
    l.push_back(&a2);
    l.push_back(&a3);
    l.push_back(&a4);
    
    return l;
}
int main() {
    
    
    vector<Smoothy*> list;
        
   // Here when i call func() objects are distroyed
    list = func();

    return 0;
}

In func you are storing the address of function local variables in l . So when you return l from the function, all the Smoothy* are now pointing to invalid memory.

To fix this, you can allocate memory for each pointer you add to l , like this:

l.push_back(new Smoothy{a1});  // instead of  l.push_back(&a1);
// etc. for a2, a3, ...

To really get away from this problem, consider not using pointers at all. If your design doesn't need it, you can get rid of the pointers, and you'll save yourself a lot of trouble.

Well, when a method returns, of course all local/automatic variables are destroyed. Under the late revision c++ changes, there is the return && modifier, which invokes move semantics, which means for not const local/automatic objects you return, it steals: clones the returned object, making a new object and copying all the primitives and object pointers, then sets the object pointers to null so they cannot be deleted/freed by the destructor. (Note that C free of a null pointer does nothing,) For const, of course. it must deep copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM