我们经常通过引用传递字符串对象,但很少通过value.today,当gdbing一个func调用时,我很困惑通过传递带有value.my代码的字符串对象:
fun1(string val,string& ref){
printf("any thing....");
}
fun2(){
std:string val = "abc";
std:string ref = "abc";
fun2(val,ref);
}
在gdb中:
p ref
$1 = (string &) @0x7fff61f28a30: {
static npos = 18446744073709551615,
_M_dataplus = {
<std::allocator<char>> = {
<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>},
members of std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Alloc_hider:
_M_p = 0x1f230078 "abc"
}
}
p val
$2 = {
static npos = 18446744073709551615,
_M_dataplus = {
<std::allocator<char>> = {
<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>},
members of std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Alloc_hider:
_M_p = 0x7fff61f28a40 "?
}
}
我的问题:
答案 0 :(得分:0)
代码中有一些错误,请尝试以下方法:
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
void fun1(string val, string & ref)
{
printf("any thing....\n");
printf("%s\t%s", val.c_str(), ref.c_str());
}
void fun2()
{
string val = "val";
string ref = "ref";
fun1(val, ref);
}
int main(int argc, char ** argv)
{
fun2();
return EXIT_SUCCESS;
}
您了解此代码中发生了什么吗? 这需要解释吗?