问题背景: while(!AQueue.empty()) { AType *pa = AQueue.front(); xxx_free(pa->element);//这里是调用私有的free完成必要的释放 delete pa; AQueue.pop_front(); } 针对上述的实现,有个疑问:pop_front会不会删除并销毁队头元素?自 己delete后,再调用pop_front不是会重复释放? 试验程序:myqueue.cpp #include <iostream> #include <queue> class A { public: A(){std::cout << "A constructor " << ' ';} ~A(){std::cout << "A destructor " << ' ';} void fun(); }; void A::fun() { std::cout << "A::fun()" << ' '; } int main() { std::queue<A> myqueue; A a1; A a2; myqueue.push(a1); myqueue.push(a2); a1.fun(); a2.fun(); myqueue.pop(); a1.fun(); a2.fun(); return 0; } 编译:g++ -g -Wall -std=c++11 myqueue.cpp 打印: A constructor A constructor A::fun() A::fun() A destructor A::fun() A::fun() A destructor A destructor A destructor 实验一:修改程序如下 #include <iostream> #include <queue> class A { public: A(){std::cout << "A constructor " << ' ';} ~A(){std::cout << "A destructor " << ' ';} void fun(); }; void A::fun() { std::cout << "A::fun()" << ' '; } int main() { std::queue<A> myqueue; A a1; //A a2; myqueue.push(a1); //myqueue.push(a2); //a1.fun(); //a2.fun(); myqueue.pop(); a1.fun(); //a2.fun(); return 0; } 打印: A constructor A destructor A::fun() A destructor 实验二:修改程序如下 #include <queue> class A { public: A(){std::cout << "A constructor " << ' ';} ~A(){std::cout << "A destructor " << ' ';} void fun(); }; void A::fun() { std::cout << "A::fun()" << ' '; } int main() { std::queue<A*> myqueue; A *pa1 = new A(); myqueue.push(pa1); std::cout << "myqueue push pa1" << ' '; myqueue.pop(); pa1->fun(); return 0; } 打印: A constructor myqueue push pa1 A::fun() //上面的实验二有内存泄漏,并且没有调用析构函数 实验三:修改程序如下 #include <iostream> #include <queue> class A { public: A(){std::cout << "A constructor " << ' ';} ~A(){std::cout << "A destructor " << ' ';} void fun(); }; void A::fun() { std::cout << "A::fun()" << ' '; } int main() { std::queue<A*> myqueue; A *pa1 = new A(); myqueue.push(pa1); std::cout << "myqueue push pa1" << ' '; std::cout << "before pop myqueue.size()=" << myqueue.size() << ' '; myqueue.pop(); std::cout << "after pop myqueue.size()=" << myqueue.size() << ' '; pa1->fun(); if(pa1 != nullptr) { std::cout << "delete pa1" << ' '; delete pa1; pa1 = nullptr; } return 0; } 打印: A constructor myqueue push pa1 before pop myqueue.size()=1 after pop myqueue.size()=0 A::fun() delete pa1 A destructor 实验四:修改程序如下 #include <iostream> #include <queue> class A { public: A(){std::cout << "A constructor " << ' ';} ~A(){std::cout << "A destructor " << ' ';} void fun(); }; void A::fun() { std::cout << "A::fun()" << ' '; } int test() { std::queue<A*> myqueue; A *pa1 = new A(); myqueue.push(pa1); std::cout << "myqueue push pa1" << ' '; std::cout << "before pop myqueue.size()=" << myqueue.size() << ' '; myqueue.pop(); std::cout << "after pop myqueue.size()=" << myqueue.size() << ' '; pa1->fun(); /*if(pa1 != nullptr) { std::cout << "delete pa1" << ' '; delete pa1; pa1 = nullptr; }*/ return 0; } int main() { test(); std::cout << "test out" << ' '; return 0; } 打印: A constructor myqueue push pa1 before pop myqueue.size()=1 after pop myqueue.size()=0 A::fun() delete pa1 A destructor test out