I have a code like:
class A{
public:
void method1 (){
// do something
}
};
class B{
public:
void method2 (){
// do something
}
};
main(int argc, char* argv[])
{
A a ;
a.method1();
// free object a
B b ;
b.method2();
}
Now, before creating b object, I want to free memory alocated by a. Can anybody help me how to do that ?
You just need to add additional scope
{,}, Sinceais a automatic/local object, it will be destroyed automagically once the scope in which it is declared ends.This is also popularly known as RAII in C++.