C ++中的临时对象确实是const吗? [英] Are temporary objects in C++ const indeed?

查看:108
本文介绍了C ++中的临时对象确实是const吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为C ++中的临时对象会被编译器自动视为const。但是最近我经历了以下代码示例:

I always believed that temporary objects in C++ are automatically considered as const by the compiler. But recently I experienced that the following example of code:


function_returning_object()。some_non_const_method();

对C ++编译器有效。这让我感到奇怪-C ++ const中的临时对象的确是吗?如果是,那么为什么编译器认为上面的代码正确?

is valid for C++ compiler. And it makes me wonder - are temporary objects in C++ const indeed? If yes, then why the code above is considered correct by the compiler?

推荐答案

否,不是。除非您将返回类型声明为const。

No, they're not. Not unless you declare the return type as const.

这篇关于C ++中的临时对象确实是const吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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