2019.11.05 C++编译warning:Reference to stack memory associated with local variable 'xxx' returned

函数返回的是一个局部变量的引用,局部变量会在结束时销毁,因此引用可能出错

template <class T>
const T& QStack<T>::topValue(QStack<T>* s) const {
    T temp = s->pop();
    s->push(temp);
    return temp;    // 这个地方warning的原因是:返回了局部变量的引用,局部变量会在结束时销毁
}
template <class T>
const T& QStack<T>::topValue(){
    return topValue(this);
}
发布了145 篇原创文章 · 获赞 21 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43826242/article/details/102911927
今日推荐