I have to call a function of a class from another function of the same class. But the problem is for some cases the current object can be destroyed. So for this case calling the function is creating errors. Is there any way to check whether the current object is destroyed or not from inside the class?
Another thread may first delete the current object and then create another object of the same class before calling function1
from inside function2
. In that case, calling function1
would be valid, right?
For example,
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
class example {
public:
example(){}
~example() {}
void function1() {
// some code here
return;
}
void function2() {
//some code here
// I want to check whether the current object is destroyed or not before calling function1()
function1();
}
private:
bool check;
};
int main()
{
example *exp=new example();
exp->function2();
}