Did a search on this and didn't come up with anything, and it's starting to baffle me. I was working on some class functions that require a pointer to another class. I was originally implementing as below, but then changed it to the second chunk of code. When I changed it to the second I noticed my values were now gone, and my objects no longer existed.
So when you use a function in this manner, myFunction(new myClass()); does the pointer to the newly created myClass get deleted after the function returns?
Code:
// this is the way i implemented it, and stored values,
// using assign(new someOtherClass()); works fine in this case
// and 'data' retains the information, but what happens to
// the pointer to the newly made someOtherClass? Memory leak???
class myClass
{
private:
someOtherClass data;
public:
// ignore that there are no constructors
void assign(someOtherClass* otherClass)
{
data = (*otherClass);
}
};
Code:
// this is the second way I implemented it, thinking it was better,
// but 'data' ends up not retaining the pointer after the
// assign(new someOtherClass()) function is called, what gives???
class myClass
{
private:
someOtherClass* data;
public:
// ignore that there are no constructors
void assign(someOtherClass* otherClass)
{
if (data != NULL) delete data;
data = otherClass;
}
};