My understanding is that variables that are created inside a while or for loop disposed/deleted from memory after the loop is done executing (see reference 1). But why does the following code show the variable is using the value from PREVIOUS loop? Is that is because stringstream variable have some special power?
The following is my C++ code snippet:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main(){
string str="1A2";
stringstream ss(str);
int intVar;
while(ss.good()){
char charVar;
ss >> intVar;
ss>> charVar;
cout<<"intVar="<<intVar << " charVar="<<charVar<<endl;
}
}
The following 2 lines are actual output:
intVar=1 charVar=A
intVar=2 charVar=A
Why it is not the following 2 lines?
intVar=1 charVar=A
intVar=2 charVar=