This question already has an answer here:
- What is The Rule of Three? 8 answers
I am trying to store class objects into an std::list, however upon access the program crashes: Error in ./main: double free or corruption (fasttop): 0x0000000002145150 *. I would like to know how to properly implement this correctly and also whether dynamically allocating the struct inside the class and exposing the pointer via the getResourcePtr() method is a good idea at all.
The class definition:
class structClass {
public:
structClass(const double &value);
~structClass();
struct requestedResources
{
double Item;
};
requestedResources *getResourcePtr() const;
private:
requestedResources *resourcePtr;
};
The class implementation:
structClass::structClass(const double &value)
{
resourcePtr = new struct requestedResources;
resourcePtr->Item = value;
}
structClass::~structClass()
{
delete resourcePtr;
}
structClass::requestedResources *structClass::getResourcePtr() const
{return resourcePtr;}
The main code:
std::list<structClass> jobs;
structClass task2(5);
cout << task2.getResourcePtr()->Item << endl;
jobs.push_back(task2);
cout << jobs.front().getResourcePtr()->Item << endl;
Aucun commentaire:
Enregistrer un commentaire