e.g.
#include <iostream>
class JMP
{
public:
std::string number;
JMP() {}
JMP(const char* num) { number = num; }
JMP operator+(const JMP &j)
{
// Allocate object
JMP *sum_obj = new JMP("0");
sum_obj->number[0] += 1;
return *sum_obj;
} // <--- memory leak
};
int main()
{
JMP *a = new JMP(), *b = new JMP(), *c = new JMP();
*c = *a + *b;
delete a; delete b; delete c;
return 0;
}
How I can delete the allocated sum_obj
object?
Can using smart pointers help here?
Notice: I am doing this object for heavy math calculations, please don't suggest using stack memory.
Any reference or helpful answer would be appreciated.