引用的参考文献 [英] REFERENCES REVEALED

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

问题描述



我完全理解变量/对象和指针变量:


int X = 5;


int * pX =& X;


* pX = 4;


int ** ppX =& pX:


** ppX = 3;

---


我确切知道引用的行为,但我想要要知道他们实际上是如何工作的!/ /

例如,请使用以下功能:


void Repot( unsigned int * pX)

{

* pX = 45U;

}

来电者将执行以下操作:


int main(无效)

{

unsigned int X = 17U;


Repot(& X);

}

显然有人可以将上面的代码重写为:

void Repot(unsigned int& X)

{

X = 45U;

}

int main(无效)

{

unsigned int X = 17;


Repot(X);

}

但我想知道的是w帽子实际上是在引擎盖下进行的。在我继续之前,这里有一个快速的小方注释:当我第一次学习

关于引用时,我讨厌它们,我认为它们非常愚蠢。一个

可能会说它们更容易使用,就像人们可能会说自动变速器汽车更容易驾驶,但长话短说,我

更喜欢手动传输和讨厌引用。所以无论如何,我找不到引入C ++引用的理由,而不是使用
使运算符重载为类。

我听到了Bjarne自己的一句话,说他之所以引入C ++的引用是为了让运算符重载为类。公平

够了,虽然在我看来这是一个很大的设计,但它的确有效!只是

澄清,这里是我讨厌引用的内容:之前,当

调用一个函数时,你可以指定X。表示存储在

变量中的值,你可以指定& X。指示变量的内存地址

。有了参考文献,现在已不再如此。 (是的,我知道

可以从函数原型中确定它是否是b
参考...我不喜欢它)。


无论如何,编译器如何处理引用?它只是简单地将它们视为短手吗?编译器会将我的前一代码(使用引用写成

)转回指针吗?即。它会简单地插入*

和&在适当情况下?如果答案是肯定的,那么我按下

,两个代码样本都会编译相同,因此分配相同数量的内存。

。 (也就是说,使用引用并不会压制

需要另外一个变量来存储另一个

变量的地址,即指针变量) 。如果是这样的话,一切都很好,而且无论我是否喜欢它,我都会理解参考资料。如果

这不是这样,我不知所措!之前,我曾经把它想象成

如下:如果一个函数有一个参考作为参数,那么你可以把它想象成它作为变量的范围被扩展到已被调用的函数。这个概念对我来说很好,直到我遇到函数

,其返回类型是一个参考。这就是我的大脑开始的时候。
口吃。采取函数原型:


unsigned int& TeachFarmAnimals(void);


当你调用上面的函数时,你究竟得到了什么?

返回类型?!!你只是收到一个指针,并且是编译器只是

坚持在*中和&在适当的地方为你?

总结:


引用只是指针而不需要包含&和*,以及

编译器只处理&和*为你在后台?

-JKop


I understand variables/objects and pointer variables perfectly:

int X = 5;

int* pX = &X;

*pX = 4;

int** ppX = &pX:

**ppX = 3;
---

I know exactly how references "behave", but I want to know how they actually
work!

For instance, take the following function:

void Repot(unsigned int* pX)
{
*pX = 45U;
}
The caller will do the following:

int main(void)
{
unsigned int X = 17U;

Repot(&X);
}
Obviously one could rewrite the above code as:
void Repot(unsigned int& X)
{
X = 45U;
}
int main(void)
{
unsigned int X = 17;

Repot(X);
}
But what I want to know is what''s actually going on under the hood. One
quick little side note here though before I continue: When I first learned
about references, I detested them, I thought they were extremely stupid. One
may argue that they''re easier to use, just as one may argue that an
automatic transmission car is easier to driver, but long story short, I
prefer manual transmission and detest references. So anyway, I could find no
justification for the introduction of references into C++, other than to
make operator overloading for classes possible. I was pleasantly amused when
I heard a quote from Bjarne himself stating that the reason he introduced
references into C++ was to enable operator overloading for classes. Fair
enough, and although in my opinion it is heavily contrived, it works! Just
for clarification, here''s what I hate so much about references: Before, when
calling a function, you could specify "X" to indicate the value stored in
the variable, and you could specify "&X" to indicate the memory address of
the variable. With references, this is no longer so. (Yes, I am aware that
one can determine from the function prototype whether or not it''s a
reference... I don''t like it).

So anyway, how does the compiler deal with references? Does it simply treat
them as short-hand? Would the compiler turn my preceeding code (written
using references) back into pointers? ie. would it simply stick in the "*"
and "&" where appropriate? If the answer to this is Yes, then I pressume
that both samples of code would compile identically and hence allocate the
same amount of memory. (That is, the use of a reference does not supress the
need to have another variable within which to store the address of another
variable, ie. a pointer variable). If this is so, everything is fine and
dandy and I understand references, regardless of whether I like them. If
this is NOT so, I am at a loss! Before, I used to just think of it as
follows: If a function has a reference as a parameter, then you can think of
it as the scope of the variable being extended to the function which has
been called. This concept worked fine for me until I came across functions
whose return type was a reference. This is where my brain started
stuttering. Take the function prototype:

unsigned int& TeachFarmAnimals(void);

When you call the above function, what exactly are you getting back as a
return type?!! Are you simply receiving a pointer, and is the compiler just
sticking in the "*" and "&" for you where appropriate?
In summation:

Are references just pointers without the need to include "&" and "*", and is
the compiler just handling the "&" and "*" for you in the background?
-JKop

推荐答案

On Sun,2004年5月16日19:19:54 GMT, JKop< NU ** @ NULL.NULL>写在

comp.lang.c ++:
On Sun, 16 May 2004 19:19:54 GMT, JKop <NU**@NULL.NULL> wrote in
comp.lang.c++:

我完全理解变量/对象和指针变量:

int X = 5;

int * pX =& X;

* pX = 4;

int ** ppX =& pX :

** ppX = 3;

---

我确切知道引用行为的方式,但我想知道如何他们实际上工作了!


他们的工作非常好,谢谢。


[snip]

但我想知道的是实际上是在幕后发生了什么。

I understand variables/objects and pointer variables perfectly:

int X = 5;

int* pX = &X;

*pX = 4;

int** ppX = &pX:

**ppX = 3;
---

I know exactly how references "behave", but I want to know how they actually
work!
They work very well, thank you.

[snip]
But what I want to know is what''s actually going on under the hood.




不幸的是,你在错误的地方提问。 C ++标准

没有具体说明引擎盖下发生了什么。标准定义了程序员的接口以及正确使用时的行为。

它对实现没有任何要求

提供正确的结果。也没有必要让人知道编写正确的C ++程序的细节。


如果你想了解特定的编译器如何做这些事情,

研究它生成的目标代码。如果你想讨论一般的

可能的机制,请尝试新闻:comp.compilers。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



Unfortunately, you are asking in the wrong place. The C++ standard
does not specify what goes on "under the hood". The standard defines
the interface to the programmer and the behavior when used correctly.
It places no requirements at all on the implementation as to how it
delivers the correct results. Nor is it necessary to for one to know
the details to write a correct C++ program.

If you want to understand how a specific compiler does these things,
study the object code it generates. If you want to discuss the
possible mechanisms in general, try news:comp.compilers.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html




" JKop" < NU ** @ NULL.NULL>在消息中写道

news:u9 ****************** @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> wrote in message
news:u9******************@news.indigo.ie...

我完全理解变量/对象和指针变量:

int X = 5;

int * pX =& X;

* pX = 4;

int ** ppX =& pX:

** ppX = 3;

---

我确切地知道引用行为的方式,但我想知道它们是如何实际工作的!

例如,采取以下功能:

void Repot(unsigned int * pX)
{
* pX = 45U;
}

来电者将执行以下操作:

int main(void)
{
unsigned int X = 17U;

Repot(& X);
}

显然有人可以将上面的代码重写为:

void Repot(unsigned int& X)
{
X = 45U;
}

int main(void)
{
unsigned int X = 17;

Repot(X);
}

但是什么我想知道的是什么'引擎盖下5事实上回事。在我继续之前,这里有一个快速的小注意事项:当我第一次学习
关于引用时,我讨厌它们,我认为它们非常愚蠢。
有人可能会说它们更容易使用,就像人们可能会说自动变速器汽车更容易驾驶,但长话短说,我更喜欢手动变速器和讨厌引用。所以无论如何,我发现
没有理由将引用引入C ++,除了使类的运算符重载成为可能。当我听到Bjarne自己引用他引用C ++引用的原因是为了使类的操作符重载时,我很高兴
。公平
足够,虽然在我看来它是非常人为的,但它的确有效!只是为了澄清,这里是我讨厌引用的内容:在调用函数之前,
,你可以指定X。表示存储在变量中的值,你可以指定& X。指示变量的内存地址。有了参考文献,现在已不再如此。 (是的,我知道
可以从功能原型中确定它是否是一个
参考...我不喜欢它)。


你错过了参考文献的两个重要方面,一个是审美,一个是实际的b $ b $

从美学的角度来看,指针和

引用之间的主要区别在于引用始终引用同一个对象,unline

指针。所以如果你需要引用某个对象,那个引用(在

一般意义上)永远不会在其生命周期中引用另一个对象

你应该考虑使用一个参考(在C ++意义上),因为它更好

表达你正在做的事情。


从实际的角度来看,有一个事实是一个const引用

可以绑定到临时,指针不能做的事情。考虑


class X;


void f1(const X *);

void f2(X);

void f3(const X&);

X g();


f1(& g()); //非法


X t = g();

f1(& t); //丑陋的临时变量,额外的副本


f2(g()); //额外的副本,可能会也可能不会被编译器优化


f3(g()); //完美


无论如何,编译器如何处理引用?它只是
将它们视为短手吗?编译器会将我的前置代码(使用引用编写)转回指针吗?即。它会简单地粘在*和&中吗?在适当的地方?

I understand variables/objects and pointer variables perfectly:

int X = 5;

int* pX = &X;

*pX = 4;

int** ppX = &pX:

**ppX = 3;
---

I know exactly how references "behave", but I want to know how they actually work!

For instance, take the following function:

void Repot(unsigned int* pX)
{
*pX = 45U;
}
The caller will do the following:

int main(void)
{
unsigned int X = 17U;

Repot(&X);
}
Obviously one could rewrite the above code as:
void Repot(unsigned int& X)
{
X = 45U;
}
int main(void)
{
unsigned int X = 17;

Repot(X);
}
But what I want to know is what''s actually going on under the hood. One
quick little side note here though before I continue: When I first learned
about references, I detested them, I thought they were extremely stupid. One may argue that they''re easier to use, just as one may argue that an
automatic transmission car is easier to driver, but long story short, I
prefer manual transmission and detest references. So anyway, I could find no justification for the introduction of references into C++, other than to
make operator overloading for classes possible. I was pleasantly amused when I heard a quote from Bjarne himself stating that the reason he introduced
references into C++ was to enable operator overloading for classes. Fair
enough, and although in my opinion it is heavily contrived, it works! Just
for clarification, here''s what I hate so much about references: Before, when calling a function, you could specify "X" to indicate the value stored in
the variable, and you could specify "&X" to indicate the memory address of
the variable. With references, this is no longer so. (Yes, I am aware that
one can determine from the function prototype whether or not it''s a
reference... I don''t like it).
You''re missing out on two important aspect of references, one aesthetic, one
practical.

From the aesthetic point of view the chief difference between pointers and
references is that references always refer to the same object, unline
pointers. So if you need to refer to some object, and that reference (in the
general sense) is never going to refer to another object during its lifetime
you should consider using a reference (in the C++ sense) because it better
expresses what you are doing.

From the practical point of view there is the fact that a const reference
can bind to a temporary, something a pointer cannot do. Consider

class X;

void f1(const X*);
void f2(X);
void f3(const X&);
X g();

f1(&g()); // illegal

X t = g();
f1(&t); // ugly temp variable, extra copy

f2(g()); // extra copy, may or may not be optimised by compiler

f3(g()); // perfect


So anyway, how does the compiler deal with references? Does it simply treat them as short-hand? Would the compiler turn my preceeding code (written
using references) back into pointers? ie. would it simply stick in the "*"
and "&" where appropriate?




几乎可以肯定的是,我确定我一直认为这是每个编译器上的

案例我'曾经使用过,并没有真正打扰过

检查。唯一可以确定的方法是查看编译器生成的机器代码




john



Almost certainly, so certain am I that I''ve always assumed this to be the
case on every compiler I''ve ever used, and haven''t actually bothered to
check. The only way to be sure would be to have a look at the machine code
your compiler generates.

john





我终于理解了参考变量!这是他们向我描述的让我感到困惑的方式。我从

学到的书从未明确说过:


参考变量只是一个指针变量。

引用变量只是一个指针变量。

引用变量只是一个指针变量。

它就像一个const指针变量,即。你必须在

声明初始化它:


int m = 5;


int * const pM =& ; m;


int& j = m;

Andt的第二个也是最后一个区别是你把它当成一个普通的

变量,即。你不要用星号来访问它的数据。再次,像

a正常变量,如果你得到它的地址,你会得到一个int *,因为

反对一个int * * QUOT ;.我现在脑子里很清楚。我完全理解:


Car&保时捷= *(新车);


删除&保时捷;

它比它更漂亮:

Car * pPorsche =新车;


删除pPorsche;

也就是说,你可以使用Car,而不是使用Car *!!

类似地,当函数返回引用时:


int& GetApe(void);

它返回一个const指针,你可以将其视为int而不是

和int *。

John Harrison,你说过引用也可以绑定到临时的,并且

这也很酷,但为了清晰起见,指针也是如此。考虑以下测试代码



长双猴(无效)

{

返回87.343;

}


void巧克力(长双* pTermite)

{

* pTermite = 56.242;


std :: cout<< * pTermite;

}


int main(无效)

{

长双k;


巧克力(&(k = Monkey()));


system(PAUSE);

返回0;

}


上面的临时有效,对吧?

-JKop


这篇关于引用的参考文献的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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