在C ++中,通过引用扩展范围是否安全? [英] In C++, is it safe to extend scope via a reference?

查看:125
本文介绍了在C ++中,通过引用扩展范围是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,是否可以通过引用扩展范围?

In C++, is it safe to extend scope via a reference?

在代码中,我的意思是:

In code, what I mean is:

MyCLass& function badIdea()
{
    MyClass obj1;
    ...
    return obj1;
}


推荐答案

范围通过引用。 C ++中的对象不被引用计数,当obj1超出范围它会被删除,参考badIdea()的结果只会让你陷入麻烦

It is NOT safe to extend the scope via reference. Objects in C++ are not reference counted when obj1 goes out of scope it will be deleted, refering to the result of badIdea() will only get you into trouble

这篇关于在C ++中,通过引用扩展范围是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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