gpt4 book ai didi

C++ 文字字符串和常量字符串引用参数

转载 作者:可可西里 更新时间:2023-11-01 17:57:36 28 4
gpt4 key购买 nike

<分区>

我有两个重载函数,分别是 const std::string& 和 bool。我现在用文字字符串调用函数。 bool 版本被调用。这有点奇怪,而且确实是一个陷阱。

谁能解释一下为什么?

请参阅下面的代码。输出是

Write == 1

#include <iostream>
#include <string>

void write(const std::string& name_) {
std::cout << "Write == " << name_ << std::endl;
}

void write(bool name_) {
std::cout << "Write == " << name_ << std::endl;
}

int main()
{
write("data");
}

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