I'm having trouble because of a feature of c++. after working with dynamic memory allocation, I always clear the heap(free store) because of obvious reasons. I do it with the destructor function. And sometime, allocate memory using the constructor. but after calling an object from another function(call by value), only the destructor works. I know that it's a good feature in many cases. But, it is annoying while working with a lot of stand-alone functions. Any way around it,guys? Or, should i just use a member function to allocate and clear memory?
An example:
#include <iostream>
using namespace std;
class test{
int *a;
public:
test(int x) {a=new int;
*a=x;}
int geta(){return *a;}
~test(){delete a;}
};
int apow2(test t)
{
return ((t.geta())*(t.geta()));
}
int main()
{
test tst(10);
cout<<"\nA^2="<<apow2(tst);
//at this point, memory reference to 'a' doesn't exist
cout<<"\n"<<tst.geta();
return 0;
}