c++ - CPP, Variables don't go out of scope -
in other experiments have done, variables go out of scope intended, when put variables in main method, don't go out of scope or that's seems because destructor never gets called:
#include <string> #include <iostream> using namespace std; #define print(s) cout << s #define println(s) print(s) << endl class animal { public: string name; int legs; animal(string name, int legs) { this->name = name; this->legs = legs; } ~animal(){ println("deleting"); } static animal createanimal() { println("creating"); return animal("animal", 4); } }; int main() { println("start"); animal = animal::createanimal();//or animal a("hello", 5); println("end running method"); println("end"); system("pause"); return 0; //should print out "deleting" here //because of going out of scope }
at point in code have comment
//should print out "deleting" here //because of going out of scope
"a" isn't out of scope. go out of scope application terminates. other similar questions (are destructors run when calling exit()?) discuss can expect when application terminates.
Comments
Post a Comment