简体   繁体   中英

c++ std::stack content will not be free when leaving function

I think in function err_fun, the stack em will be free when leave err_fun(), so the content of em will be free in the same time, and why i can still get right answer in main function with stack st?

Thanks.

#include <stack>

stack<int> st;

void err_fun() {
    stack<int> em;
    st.swap( em );
}

int main() {
    err_fun();

    //... some operation with stack st.
    return 0;
}

The content of em will be copied over as that's what you asked for when you did swap . Since st is global in scope it's not destroyed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM