gpt4 book ai didi

c++ - 构造 const 对象与对 const 对象的引用

转载 作者:太空宇宙 更新时间:2023-11-04 14:52:36 25 4
gpt4 key购买 nike

我对最佳实践很好奇。假设我需要构造一个不应进一步修改的对象:

  void fn() {
const std::string& ref_to_const = "one";
...
const std::string const_object = "two";
...
}

临时对象和常量对象的生命周期是一样的。那么使用什么方法呢?

最佳答案

So what approach to use?

通常,更具表现力的是:普通的const std::string

常量引用可以绑定(bind)到临时值并延长其生命周期这一事实的存在主要是为了使临时值的引用调用成为可能。没有理由在像您这样的情况下使用它。

关于c++ - 构造 const 对象与对 const 对象的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49365299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com