Why does value k
change, after the second call of check(base)
?
class Base{
public:
int k=0;
virtual void print(){
cout<< k<<endl;
}
};
Base* check(Base* p){
Base* t = new Base;
t->k = 4;
delete p;
return t;
}
int main(){
Base* base = new Base;
cout<< check(base)->k; // first call, print 4
cout<<"\n";
cout<<base->k;// print 0
check(base); // second call
cout<<"\n";
cout<< base->k; // print 4
cout<<"\n";
return 0;
}
Ouput:
4
0
4
Aucun commentaire:
Enregistrer un commentaire