[英]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上没有太多提及,而且我发现的唯一提及对我来说太技术性了。
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>
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. 请小心将references
或pointers
传递给新线程,因为您需要确保在新线程正在使用它们时它们保持有效。 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.