After member function delete the current instance,how to stop the behind code executes.
See the code.
#include <iostream>
class A;
void callfun(int i,A *pt);
class A {
public:
A() { sss="this is A."; }
virtual ~A() {}
void foo(int i) {
callfun(i,this); //call a function.Don't return value.Maybe delete instance.
out();
}
private:
void out() {
std::cout<< "Out:" <<std::endl;
std::cout<< sss << std::endl;
}
std::string sss;
}
void callfun(int i,A *pt) {
if (i==0)
delete pt; //If conditions are matched,delete instance.
}
int main() {
A *a1=new A;
a1->foo(1); //print:Out:this is A.
a1->foo(0); //delete a1,don't print.But in fact it would print.how to do?
}
I want the result:foo(1) output “Out:this is A.”,foo(0) delete instance,don’t output.
The following is a more complete code.