返回对局部变量的引用 [英] Returning reference to a local variable

查看:103
本文介绍了返回对局部变量的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这段代码在Code :: block中成功运行。 IDB只是报告

Why can this code run successfully in Code::block. The IDB just reports


警告:引用本地变量'tmp'返回,

warning: "reference to local variable ‘tmp’ returned",

但输出结果hello world成功。

but ouput the result "hello world" successfully.

#include <iostream>
#include<string>
using namespace std;

const string &getString(const string &s)
{
    string tmp = s;
    return tmp;
}

int main()
{
    string a;
    cout<<getString("hello world")<<endl;
    return 0;
}


推荐答案

也许此链接将帮助您。

这篇关于返回对局部变量的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆