指针与参考文献:关于风格的问题 [英] Pointers vs References: A Question on Style

查看:82
本文介绍了指针与参考文献:关于风格的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过像Scott Meyer的EC ++(第22项)这样的文章,主张在传递参数时使用

的引用。我理解

使用引用背后的原因 - 当你通过引用传递一个对象时,你可以避免创建本地对象的成本。


但是为什么要使用参考?使用

引用比使用指针有任何固有的优势吗?在参数传递方面,我的工作场所不鼓励使用

指针。他们总是喜欢参考。


我认为我们考虑使用指针的一个原因是帮助

沟通意图。例如,假设我在C中执行以下操作:


#include" foo.h"


int main(int argc,char * argv [])

{

int x = 5;


foo(x);


printf("%d \ n",x);


返回0;

}


我可以保证程序将打印''5''因为C只支持

传值。如果我们使用C ++,我就不能保证,因为

我们被允许通过引用传递。


但是如果我在参数传递中使用指针,我可以使用

运算符的地址来向其他程序员传达意图

意图被修改的意图。例如,


#include" A.h"

#include" X.h"


int main(int argc,char * argv [])

{

A a;

X x;


x.foo(& a); //这个成员函数的意图是修改''a''

//将调用'a'的非const成员函数。


x.bar(a); //这个成员函数不会修改''a''。只有const

//'a''的成员函数才会被调用。


std :: cout<< a<< std :: endl;


返回0;

}


如果我遇到对象问题''a'',我看到那个成员函数栏()

不会改变对象''a''(按惯例),而且我的问题很可能是

在成员函数foo()中。如果我通过大量代码筛选来跟踪问题,这将特别有用。通过使用这个约定,我可以节省时间,而不必查看类

声明中的每个成员函数,看看哪些可能是我问题的原因。


当然,所有程序员都必须遵守约定才能使它有用。所以长话短说,这样做更好吗

这个:


void x :: foo(A * a); // foo()将修改''a''

void x :: bar(const A& a); // bar()不会修改''a''


或者我的工作场所需要做的更好吗?


void x :: foo(A& a); // foo()将修改''a''

void x :: bar(const A& a); // bar()不会修改''a''


我建议使用指针进行参数传递我错了吗?

有没有理由为什么引用总是优先于指针?

我的想法是这个约定会使C ++代码更具可读性。
< br $> b $ b ----

德斯蒙德


(从电子邮件回复的地址中删除''nospam'',但是我更喜欢

回复帖子到新闻组)

I''ve read articles like Scott Meyer''s EC++ (Item 22) that advocate the use
of references when passing parameters. I understand the reasoning behind
using references--you avoid the cost of creating a local object when you
pass an object by reference.

But why use a reference? Is there any inherent advantage of using a
reference over using a pointer? My workplace discourages the use of
pointers when it comes to parameter passing. They always prefer references.

I would argue one reason why we might consider using a pointer is to help
communicate intent. For example, suppose I do the following in C:

#include "foo.h"

int main(int argc, char *argv[])
{
int x=5;

foo(x);

printf("%d\n",x);

return 0;
}

I can guarantee that the program will print ''5'' because C only supports
pass-by-value. If we''re using C++, I can''t make that guarantee because
we''re allowed to pass-by-reference.

But if I use pointers in parameter-passing, I can use the address-of
operator to communicate intent to other programmers that the parameter
is meant to be modified. For example,

#include "A.h"
#include "X.h"

int main(int argc, char *argv[])
{
A a;
X x;

x.foo(&a); // the intent of this member function is to modify ''a''
// A non-const member function of ''a'' will be called.

x.bar(a); // this member function will NOT modify ''a''. Only const
// member functions of ''a'' will be called.

std::cout << a << std::endl;

return 0;
}

If I''m having problems with object ''a'', I see that member function bar()
will not change object ''a'' (by convention), and that my problem is likely
in member function foo(). This is especially useful if I''m sifting through
a large amount of code to track down a problem. By using this convention, I
can save time not having to look at every member function in the class
declaration to see which ones might be the cause of my problem.

Of course, all programmers have to adhere to the convention in order for it
to be useful. So to make a long story short, is it better to do
this:

void x::foo(A* a); // foo() will modify ''a''
void x::bar(const A& a); // bar() will not modify ''a''

Or is it better to do what my workplace requires?

void x::foo(A& a); // foo() will modify ''a''
void x::bar(const A& a); // bar() will not modify ''a''

Am I wrong in suggesting the use of pointers for parameter-passing? Is
there a reason why a reference should always be preferred over a pointer?
My thinking is that this convention would make the C++ code more readable.

----
Desmond

(Remove the ''nospam'' from the address for e-mail replies, but I prefer
reply posts to the newsgroup)

推荐答案

我选择了一个指针,只有2种情况超过

参考:


1)什么时候必须重新坐下


2)当数组是涉及

There are 2 and only 2 situations in which I choose a pointer over a
reference:

1) When it must be re-seated

2) When arrays are involved

我理解
使用引用背后的原因 - 当你通过时,你避免了创建本地对象的成本一个引用的对象。
I understand the reasoning behind
using references--you avoid the cost of
creating a local object when you
pass an object by reference.



不正确。今天的计算机(即堆栈和寄存器)的性质仍然需要隐藏指针。 (这是大概功能是

所涉及的)。

-JKop


Incorrect. The nature of today''s computers (ie. stack and registers) still
require a hidden pointer. (This is ofcourse where outline functions are
involved).
-JKop


>有2和只有2种情况我在
>There are 2 and only 2 situations in which I choose a pointer over a
参考上选择一个指针:

1)什么时候必须重新安装

2)当数组时参与
reference:

1) When it must be re-seated

2) When arrays are involved




另一种可能性是函数参数是可选的。指针可以是

null而引用不能。



Another possibility is when a function argument is optional. Pointers can be
null whereas references cannot.


DaKoadMunky写道:
DaKoadMunky wrote:
我有两种情况,只有两种情况我选择一个指针而不是

1)什么时候必须重新坐下

2)当涉及数组时
另一种可能性是函数参数是可选的。指针可以
There are 2 and only 2 situations in which I choose a pointer over a
reference:

1) When it must be re-seated

2) When arrays are involved
Another possibility is when a function argument is optional. Pointers can



为null,而引用则不能。


一个好的样式规则是永远不会返回null,并且绝不接受null作为

参数。


关注通过传入Null对象来规则。考虑一下这段代码:


void funk(SimCity const * pCity)

{

if(pCity)

pCity-> throwParade();

}


现在与此形成鲜明对比:


class NullCity:公共模拟城市

{

public:/ * virtual * / void throwParade(){}

};


void funk(SimCity const& aCity)

{

aCity.throwParade();

}


代码简化了,因为它推动了界面背后的行为。

界面只是向其调用者承诺它正在做某事,无论是否b
确实如此。像这样的模式是OO的核心 - 编程接口而不是实现。


Desmond Liu写道:

但为什么要使用参考?使用
引用而不是使用指针有任何固有的优势吗?在参数传递方面,我的工作场所不鼓励使用
指针。他们总是喜欢


be null whereas references cannot.
One good style rule is to never return null, and never accept null as
parameter.

Follow that rule by passing in instead a Null Object. Consider this code:

void funk(SimCity const * pCity)
{
if (pCity)
pCity->throwParade();
}

Now contrast with this:

class NullCity: public SimCity
{
public: /*virtual*/ void throwParade() {}
};

void funk(SimCity const & aCity)
{
aCity.throwParade();
}

The code simplifies because it pushes behavior behind an interface. The
interface simply promises to its caller that it is doing something, whether
or not it really is. Patterns like this are the heart of OO - programming to
the interface instead of the implementation.

Desmond Liu wrote:
But why use a reference? Is there any inherent advantage of using a
reference over using a pointer? My workplace discourages the use of
pointers when it comes to parameter passing. They always prefer



引用。


您需要与同事配对,因为如果他们只知道

足够的C ++来写下这个建议,他们可能会更口头地知道。


C ++关键字const指示编译器拒绝公开尝试改变

a变量的价值。隐蔽尝试产生未定义的行为,意思是

任何事情都可能发生。

C ++函数可以通过复制,地址或引用来获取参数。

理想情况下,如果传递给函数的对象没有改变,则对象

应该通过副本传递:


void foo(SimCity aCity) ;


该代码效率低下。一般来说,程序员不应该强调

效率,直到他们有足够的代码来测量它并找到缓慢的
点。在这种情况下,更有效的实施是相同的成本。

当我们通过参考,我们的程序花费时间制作一个完整的城市

的大量副本:


void foo(SimCity& aCity);


现在如果foo()不会改变那个城市的价值,那么应该在其界面中声明

意图,使用pass-by-constant-reference

模拟传递副本:


void foo(SimCity const& aCity);


这是最有效的调用语法,在认知上和物理上。它是认知效率很高的因为它给foo()没有复制的对象愚蠢地改变然后丢弃。 foo()里面的声明可能会尝试改变那个城市不应该编译。它的物理效率是因为

编译器产生的操作码只给foo()一个现有城市的句柄,

而不复制它。


C ++在其合格类型之前支持资格,例如const

SimCity&。我首先尝试用最重要的部分来编写表达式。

在极少数情况下,还有一些微妙的技术原因可以写出模拟常数和模拟常数,用它后面的const。


-

Phlip
http://industrialxp.org/community/bi...UserInterfaces


references.

You need to pair-program with your colleagues, because if they know just
enough C++ to write that advice down, they probably know much more verbally.

The C++ keyword const instructs compilers to reject overt attempts to change
a variable''s value. Covert attempts produce undefined behavior, meaning
anything could happen.

C++ functions can take arguments by copy, by address, or by reference.
Ideally, if an object passed into a function does not change, the object
should pass by copy:

void foo(SimCity aCity);

That code is inefficient. In general, programmers should not stress about
efficiency until they have enough code to measure it and find the slow
spots. In this situation, a more efficient implementation is equal cost.
When we pass by reference, our program spends no time making a huge copy of
an entire city:

void foo(SimCity &aCity);

Now if foo() won''t change that city''s value, the function should declare
that intention in its interface, using pass-by-constant-reference to
simulate pass-by-copy:

void foo(SimCity const &aCity);

That is the most efficient call syntax, cognitively and physically. It''s
cognitively efficient because it gives foo() no copied object to foolishly
change and then discard. Statements inside foo() that might attempt to
change that city shouldn''t compile. It''s physically efficient because the
compiler produces opcodes that only give foo() a handle to an existing city,
without copying it.

C++ supports qualifications before their qualified types, such as "const
SimCity &". I try to write expressions with the most important part first.
There are also subtle technical reasons, in rare situations, to write
"SimCity const &", with the const after its type.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


这篇关于指针与参考文献:关于风格的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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