奇怪的退货声明 [英] Weird return statement

查看:67
本文介绍了奇怪的退货声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我遇到了以下代码片段,我想知道为什么

复制ctr在返回时被调用(而不仅仅是返回字符串语句

obj。


TIA


string PublishedProductsRepo :: CreateStatement()const {

字符串语句;


statement =" SELECT DISTINCT"

" *"

" FROM"

" map";

return string(statement);}

Hello all,

I ve come across the following code fragment and I was wondering why is the
copy ctr called on return (rather than just returning the string statement
obj.

TIA

string PublishedProductsRepo :: CreateStatement() const {
string statement;

statement ="SELECT DISTINCT "
"* "
"FROM "
"map";
return string(statement); }

推荐答案



jimjim skrev:

jimjim skrev:
大家好,

我遇到了以下代码片段,我想知道为什么
复制ctr在返回时调用(而不是只返回字符串语句
obj。


下面的代码没有多大意义。我会有写它

简单地说:


string PublishedProductsRepo :: CreateStatement ()const {

return" SELECT DISTINCT"

" *"

" FROM"

地图;

}


/ Peter
TIA

string PublishedProductsRepo :: CreateStatement( )const {
字符串语句;

statement =" SELECT DISTINCT"
" *"
" FROM"
" map" ;;
返回字符串(声明); }
Hello all,

I ve come across the following code fragment and I was wondering why is the
copy ctr called on return (rather than just returning the string statement
obj.
The code below does not make much sense. I would have written it
simply:

string PublishedProductsRepo :: CreateStatement() const {
return "SELECT DISTINCT "
"* "
"FROM "
"map";
}

/Peter
TIA

string PublishedProductsRepo :: CreateStatement() const {
string statement;

statement ="SELECT DISTINCT "
"* "
"FROM "
"map";
return string(statement); }






* jimjim:

我遇到了以下代码片段,我是想知道为什么在返回时调用
复制ctr(而不仅仅是返回字符串语句
obj。

字符串PublishedProductsRepo :: CreateStatement()const {
字符串语句;

statement =" SELECT DISTINCT"
" *"
" FROM"
" map";
返回字符串(声明);}

I ve come across the following code fragment and I was wondering why is the
copy ctr called on return (rather than just returning the string statement
obj.

string PublishedProductsRepo :: CreateStatement() const {
string statement;

statement ="SELECT DISTINCT "
"* "
"FROM "
"map";
return string(statement); }




是否调用复制构造函数取决于您的编译器和CreateStatement调用的

上下文。


代码中有很多不必要的东西,但它不会影响复制构造函数调用。重要的复制构造函数调用是什么? />
编译器的优化。


直接编码函数是


std :: string PublishedProductsRepo :: CreateStatement()const

{

return" SELECT DISTINCT * FROM map" ;;

}


注意std ::的资格。


如果这是内联的函数定义在头文件中,很可能,原始代码中缺少这样的限定条件表明有一个

using namespace std;或使用std :: string;在头文件中,

反过来表示一个不称职的程序员。换句话说,如果那些合理的假设成立,那么这不是可以学习的代码。

相反,它是代码,你可以从中学习如何/不是/做事。


例如:


*不要放置using namespace std;在一个头文件中。


*在没有需要的时候不要聪明。


*优先不要命名功能ComputeCosine何时cos将

做:为他们生产的产品命名产值函数,而不是如何。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门帖子。

问:usenet和电子邮件中最烦人的事情是什么?



Whether the copy constructor is called depends on your compiler and the
context of the CreateStatement call.

There is much that is unnecessary in the code, but it doesn''t affect
copy constructor calls. What''s important re copy constructor calls is
your compiler''s optimizations.

A straightforward coding of this function is

std::string PublishedProductsRepo::CreateStatement() const
{
return "SELECT DISTINCT * FROM map";
}

Note the qualification with "std::".

If this inline function definition is in a header file, which is likely,
the lack of such qualification in the original code indicates there is a
"using namespace std;" or "using std::string;" in the header file, which
in turn indicates an incompetent programmer. In other words, if those
reasonable assumptions hold, then this is not code to learn from.
Rather, it''s then code from which you can learn how to /not/ do things.

For example:

* Don''t ever place "using namespace std;" in a header file.

* Don''t be clever when there is no need.

* Preferentially don''t name a function "ComputeCosine" when "cos" will
do: name value-producing functions for what they produce, not how.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


文章< D _ ************* *****@text.news.blueyonder.co.uk> ;,

" jimjim" <是ne ***** @ blueyonder.co.uk>写道:
In article <D_******************@text.news.blueyonder.co.uk >,
"jimjim" <ne*****@blueyonder.co.uk> wrote:
我遇到了以下代码片段,我想知道为什么在返回时调用
copy ctr(而不是只返回字符串语句
obj。

TIA

string PublishedProductsRepo :: CreateStatement()const {
string statement;

statement =" SELECT DISTINCT" ;
" *"
" FROM"
" map";
返回字符串(声明);}
I ve come across the following code fragment and I was wondering why is the
copy ctr called on return (rather than just returning the string statement
obj.

TIA

string PublishedProductsRepo :: CreateStatement() const {
string statement;

statement ="SELECT DISTINCT "
"* "
"FROM "
"map";
return string(statement); }



编写代码的人可能很担心,因为''声明''

是暂时的,他不确定他是否正在返回临时或

没有.IE他对语言不太了解。


或者他可能是故意试图混淆代码?

-

魔术取决于传统和信仰。它不欢迎观察,

也不会从前获利另一方面,科学是基于经验的b $ b b;它可以通过观察和实验进行校正。



The person who wrote the code was probably worried because ''statement''
is a temporary and he wasn''t sure if he was returning a temporary or
not. IE he didn''t know the language very well.

Or maybe he was intentionally trying to obfuscate the code?
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.


这篇关于奇怪的退货声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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