dimanche 24 novembre 2019

Vector of class object with a pointer data member

Let's say I have a class just like this:

class example{
    double * ptr;

    public:
        example();
        ~example();
};

with methods implemented:

example::example(){
    ptr = new double;
    *ptr = 0;
}
example::~example(){delete ptr;}

If, in main, I create a std::vector<example> v; and v.push_back(example());, I just get a segmentation fault due invalid deletes. I'm not really sure what's causing this and if there's a way to fix it.

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire