c++ - Global variable not incremented (is gcc nuts?) -
i have next stupid code: ideone
can explain, why hack global counter not increment when deleting objects in std::vector ???
#include <iostream> #include <memory> #include <vector> int global; class { public: a(int i): a(i) {} ~a() { std::cout << "delete a" << std::endl; ++global; }; int foo() { homecoming a; } int a; }; int main() { int c = 0; int loops = 10; global = 0; // create vector of pointers std::vector<std::shared_ptr<a> > vec; for(int = 0; i< 10; i++) { vec.push_back( std::shared_ptr<a>(new a(i)) ); } for(int i=0; i<loops; i++) { for(auto & p : vec) { c += p->foo(); } } std::cout << " deleted:" << global << std::endl; } output:
deleted:0 delete delete
the a objects deleted after print global cout.
c++ c++11 shared-ptr
No comments:
Post a Comment