简体   繁体   English

const char&作为函数的参数

[英]const char& as an argument to a function

I need some clarification on the following blocks of code. 我需要对以下代码块进行一些说明。 I'm working on something similarly coded and I want to understand how it works. 我正在研究类似的编码,我想了解它是如何工作的。 There's not a lot of mentions on Google and the only one I found is too technical for me to understand. Google上没有太多提及,而且我发现的唯一提及对我来说太技术性了。

  1. In the following code, the thread and non-thread functions have different output. 在以下代码中,线程和非线程函数具有不同的输出。 Why is that? 这是为什么?

     #include <boost/thread.hpp> #include <iostream> #include <stdio.h> using namespace std; void fTry1(const char&); void fTry2(const char&); int main() { boost::thread tTry1, tTry2; const char *a = "efgh"; tTry1 = boost::thread(fTry1, *a); fTry2(*a); tTry1.join(); return 0; } void fTry1(const char& a) { cout << "Thread" << endl; cout << &a << endl; } void fTry2(const char& a) { cout << "Non-thread" << endl; cout << &a << endl; } 

Sample output: 样本输出:

Non-thread
efgh
Thread
e<garbage data>
  1. If I change the following line 如果我更改以下行

     cout << &a << endl; 

    to

     cout << a << endl; 

The output becomes something like 输出变为类似

Non-thread
e
Thread
e

Again, why? 同样,为什么呢?

-- -

The code I'm working on uses number 2 but the string being passed is a path to a folder, so I want the whole string, not just the first character. 我正在处理的代码使用数字2,但是传递的字符串是文件夹的路径,因此我需要整个字符串,而不仅仅是第一个字符。 But since I don't understand how it works, I won't be able to make changes to it quite easily. 但是由于我不了解它是如何工作的,因此我将无法轻易对其进行更改。

By default, the arguments passed to boost::thread will be "captured" by value . 默认情况下,传递给boost::thread的参数将被value “捕获”。 To capture them by reference , you have to use boost::ref ; 要通过reference捕获它们,必须使用boost::ref ;

tTry1 = boost::thread(fTry1, boost::ref(*a));

This is pretty much like lambda functions, for example: 例如,这非常类似于lambda函数:

const char *a = "efgh";
const char &c = *a;

auto garbage = [c]() { // c captured by value
   cout << &c << endl;
};

auto works = [&c]() { // c captured by ref
    cout << &c;
};

garbage();
works();

Note: 注意:

Be careful passing references or pointers to a new thread, because you need to make sure they remain valid while the new thread is using it. 请小心将referencespointers传递给新线程,因为您需要确保在新线程正在使用它们时它们保持有效。 In your unique case, however, that does not apply, because the string literal efgh has static duration so you are safe either way. 但是,在您的独特情况下,这并不适用,因为字符串文字efgh具有static duration因此无论哪种方式都是安全的。

The string literal in const char *a = "efgh"; const char *a = "efgh";的字符串文字const char *a = "efgh"; is compiled with a hidden terminator character '\\0' . 使用隐藏的终止符'\\0'编译。

When you pass it like *a you're dereferencing it. 当您像*a一样传递它时,您将取消引用它。 You're basically passing only a character. 您基本上只传递了一个字符。 This goes okay for the non threaded function, because if you use &a to take the address of the character a , making it a c-string, it will still point to the string literal "efgh" which includes the terminator. 这对于非线程函数来说还可以,因为如果您使用&a来获取字符a的地址(使其成为c字符串),它仍将指向包含终止符的字符串文字"efgh" cout will keep printing until it hits the terminator. cout将继续打印,直到碰到终止符。

But, I suspect, that when you pass *a to boost::thread it actually copies the character, but just the character, not the whole string. 但是,我怀疑,当您将*a传递给boost::thread它实际上是在复制字符,而只是复制字符,而不是整个字符串。 When you later turn it back into a c-style string using &a there is no more terminator character, so cout will just keep on printing on and on, and you see garbage. 当您稍后使用&a将其重新转换为c样式的字符串时&a不再有终结符,因此cout会不断地继续打印,您会看到垃圾。

When you change cout << &a << endl; 当您更改cout << &a << endl; to cout << a << endl; cout << a << endl; you're telling cout you only wnat to print 1 character. 您告诉cout您只有wnat才能打印1个字符。 So it will only print 1. 因此它只会打印1。

You taged your question with c++ , why not use a std::string instead? 您用c++标记了问题,为什么不使用std::string呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 istream::operator&gt;&gt;( const char&amp; ) 如何实现,因为没有实现这样的功能? - How does istream::operator>>( const char& ) as no such function is implemented? 如何将 const char&amp; 转换为 const std::string&amp;? - How to convert const char& to const std::string&? const char []默认函数参数 - const char[] default function argument 函数参数,const char **,临时变量 - Function argument, const char **, Temp Variable const char数组的初始化作为重载函数的参数 - Initialization of const char array as argument of an overloaded function 错误无法转换 &#39;coll&#39;(类型 &#39;std::__cxx11::list<char> &#39;) 输入 &#39;const char&amp;&#39; - error cannot convert 'coll' (type 'std::__cxx11::list<char>') to type 'const char&' 错误:从“char”类型的临时对象初始化“char&amp;”类型的非常量引用无效 - error : invalid initialization of non-const reference of type 'char&' from a temporary of type 'char' 没有匹配的函数调用&#39;std::vector &gt;::push_back(char&amp;)&#39; - no matching function for call to 'std::vector >::push_back(char&)' 为什么 data() 和 c_str() 返回 char const*,而 operator[] 返回 char&amp;? - Why do data() and c_str() return char const*, while operator[] returns char&? char的澄清&amp; - Clarification of char&
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM