成员函数删除当前实例后,如何停止后面的代码执行。查看代码。
#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?
}
我想要结果:foo(1) 输出 "Out:this is A.",foo(0) 删除实例,不输出。