为什么这样做? (返回自动字符串) [英] Why does this work? (returning automatic string)

查看:78
本文介绍了为什么这样做? (返回自动字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< cstring>

#include< iostream>

#include< string>


std :: string foo()

{

char buf [16];


strcpy(buf,这是一个测试。);


返回std :: string(buf);


}


int main()

{

std :: cout<< foo();


返回0;

}


在我看来foo应该返回垃圾。然而,这是一个测试

打印正确。

我在这里缺少什么?


提前感谢,

bryan

#include <cstring>
#include <iostream>
#include <string>

std::string foo()
{
char buf[16];

strcpy(buf, "this is a test.");

return std::string(buf);

}

int main()
{
std::cout << foo();

return 0;
}

it appears to me that foo should return garbage. however, "this is a test"
is printed correctly.
what am i missing here?

thanks in advance,
bryan

推荐答案

Bryan Bullard写道:
Bryan Bullard wrote:
strcpy(buf) ,这是一个测试。;


这里我们将''这是一个test \ 0''字符串复制到buf。 (注意,字符串

将被终止。)

返回std :: string(buf);


这里我们返回以null结尾的字符串。

}

int main()
{
std :: cout<< foo();
strcpy(buf, "this is a test.");
Here we copy ''this is a test\0'' string to buf. (Note that the string
will be null terminated.)
return std::string(buf);
And here we return the null terminated string.
}

int main()
{
std::cout << foo();




我们在这里打印它。


一切看起来都不错。为什么不能这样做?



And here we print it.

Everything looks fine to me. Why shouldn''t this work?


Bryan Bullard写道:
Bryan Bullard wrote:
std :: string foo()
{
char buf [16];
strcpy(buf,这是一个测试。);
返回std :: string(buf);

}
在我看来,foo应该返回垃圾。但是,这是
测试正确打印。
我在这里缺少什么?
std::string foo()
{
char buf[16];
strcpy(buf, "this is a test.");
return std::string(buf);

} it appears to me that foo should return garbage. however, "this is a test" is printed correctly.
what am i missing here?




Google forcopy constructor。然后通过调试器运行该代码,并在return语句中运行
跟踪以查看其中的一个。


(如果您返回buf,则介绍C文本警告,a字符数组,你得到

未定义的行为;这可能是你在这里意外预期的。)


-

Phlip



Google for "copy constructor". Then run that code thru the debugger and
trace inside the return statement to see one in action.

(Introductory C texts warn if you return buf, a character array, you get
undefined behavior; that''s probably what you accidentally expected here.)

--
Phlip


Bryan Bullard写道:
Bryan Bullard wrote:

#include< cstring>
#include< iostream>
#include< string>

std :: string foo()
{char buf [16];

strcpy(buf, 这是一个测试。;

返回std :: string(buf);


这会创建一个std :: string对象。

由于此函数的返回类型指定

std :: string(而不是引用或指针)该对象的
*副本*被传递回main。

}
int main()
{
std :: cout<< FOO();


这里从foo返回的std :: string对象
用于输出。由于foo的返回值

在其他任何地方都没有使用,foo返回的临时对象

在语句后被销毁

执行。

返回0;
}

#include <cstring>
#include <iostream>
#include <string>

std::string foo()
{
char buf[16];

strcpy(buf, "this is a test.");

return std::string(buf);
This creates a std::string object.
Since the return type of this function specifies
std::string (and not a reference or a pointer) a
*copy* of that object is passed back to main.

}

int main()
{
std::cout << foo();
and here the std::string object returned from foo
is used for doing the output. Since the return value
of foo isn''t used anywhere else, the temporary object
returned by foo is destroyed after the statement has
executed.

return 0;
}




-

Karl Heinz Buchegger kb******@gascad.at


这篇关于为什么这样做? (返回自动字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆