gpt4 book ai didi

c++ - 通过复制含义

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:23 24 4
gpt4 key购买 nike

<分区>

好的,我正在考虑以下 C++ 代码:

foo (std::string str) {
// do whatever
}

foo(const char *c_str) {
foo(std::string(c_str));
}

我看了这段代码,认为需要重写它以通过引用传递。基本上,我担心的是构造函数将被调用两次,一次是在 foo 的 const char * 版本中,另一次是当参数作为 std::string 传递给 foo 时,因为它被设置为通过拷贝传递。我的问题是:我是对的,还是 g++ 足够聪明,可以采用 c 字符串版本中的构造函数并称其为好?好像g++做不到,但我只是希望真正知道的人能澄清一下。

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