哦,上帝不再引用...... [英] Oh God not references again...

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

问题描述



当我是C ++的初学者时,我对引用的想法很挣扎。

学会了如何首先使用指针,我犹豫不决接受

只引用做他们的工作。就是这样。


就在最近,发布了一张海报,寻找参考文献的解释。


我会给我的自己的理解是否有价值。


首先,C ++标准是一个非常灵活的东西。它为实现他们喜欢的事情提供了一个自由的山峰,只要他们实现了目标就可以实现
。考虑

实例的虚函数 - 标准没有提到对象中的隐藏指针或

V表,即使我知道的所有实现都实现了

通过这些方式实现虚拟功能。


让我们从非常简单的代码开始吧:


int main()

{

int i = 5;


int& r = i;


r = 7;

}


如果有人熟悉指针,也熟悉计算机怎么样?
实际上工作(即CPU指令,寄存器等),然后他们可能会认为代码被视为编写代码:


int main()

{

int i = 5;


int * const r =& i;


* r = 7;

}


这是一种正确的思考方式吗?是的,我想。然后再次获得
,还有其他方法可以实现目标。如你所知,

编译器可能只看到r的定义。并且想一想,嗯...... r

只是我,并将其更改为:


int main()

{

int i = 5;


i = 7;

}


它甚至可以在内部实现它:


int main()

{

int i = 5;


#define ri


r = 7;


#undef r

}


谁知道?重要的是,实现准确地实现了参考行为的实现。


在传递对象和返回对象时,引用最有用,

来自函数:


void Func(int& i)

{

i = 5;

}


编译器如何编译?那么,C ++标准并没有说明如何。

在某种程度上,我认为引用是一些神奇的小东西,只需要完成

的工作。


然而,作为一个熟悉计算机实际工作方式的人,我知道必须有某种间接性(即指针)
如果函数不是内联的,则为
。因此,我认为编译器

的确如下:


void __Func(int * const p)

{

* p = 5;

}


#define Func(i)__Func(&(i))


然后,编译器可能会有一些新的方式来完成这个

,谁知道?!所有重要的是工作完成。


此外,通过引用返回对象:


int& Func( )

{

static int i;


返回i;

}


int main()

{

Func()= 7;

}


编译器如何使其工作?好吧,如果没有函数内联

涉及,那么我猜它会做类似的事情:


int * Func()

{

static int i;


return& i;

}


int main()

{

* Func()= 7;

}


这将是编译器实现其目标的一种方式。事实上,这个问题很简单,就是引用的是小精灵粉尘。

他们没有意义,他们也无法解释。他们只是按照他们的b $ b做的事情。


参考文献在某种程度上是特殊的。当您使用R值初始化参考

时,会发生一些特殊情况:


int& r = 5; / *不会编译* /


但是:


int const& r = 5; / *没问题* /


您可能认为这很奇怪 - 它怎么可能有效?怎么可能呢?
等同于:


int const * const p =&(5);


答案很简单:不是。 对const的引用如果是用b值初始化的
,则是特殊的。如果您写的话,原始行int& r = 5;被视为




int const __literal = 5;

int const& r = __literal;


现在,你可以看到临时和临时。对象与

参考的生命周期相同。


在我走之前还有一件事我会提到...有人对此感兴趣/>
以下代码:


struct MyStruct {

int& a;

double& b;

};


#include< ostream>

#include< iostream>


int main()

{

int i;双d;


MyStruct ms = {i,d};


ms.a = 5;


std :: cout<< sizeof ms<< std :: endl;

}


这个人对于为什么ms感兴趣有一个特定的大小。答:

C ++标准并不在乎。但实际上,类型MyStruct必须实现其目标。通过了解计算机的工作原理,我可以看到实现这一特定目标的最简单方法可能是内部使用指针:
$ br />
struct MyStruct {

int * const a;

double * const b;

};


#include< ostream>

#include< iostream>


int main()

{

int i;双d;


MyStruct ms = {& i,& d};


* ms.a = 5;


std :: cout<< sizeof ms<< std :: endl;

}


这将是编译器实现

MyStruct行为的一种方式;。但同样,它不必这样做。


参考是魔法般的小精灵粉尘,因为一天结束,他们没有

实际计算机科学的基础 - 他们只是做他们所做的事。


-


Frederick Gotham


When I was a beginner in C++, I struggled with the idea of references.
Having learned how to use pointers first, I was hesitant to accept that
references just "do their job" and that''s it.

Just recently, a poster posted looking for an explanation of references.

I''ll give my own understanding if it''s worth anything.

First of all, the C++ Standard is a very flexible thing. It gives a
mountain of freedom to implementations to do things however they like, just
so long as they achieve the objective. Consider virtual functions for
instance -- the Standard doesn''t mention hidden pointers within objects or
V-tables, even though all implementations that I know of achieve the
behaviour of virtual functions by these means.

Let''s start off with very simple code:

int main()
{
int i = 5;

int &r = i;

r = 7;
}

If someone is familiar with pointers, and also familiar with how computers
actually work (i.e. CPU instructions, registers, etc.), then they might
think that the code is treated as if it were written:

int main()
{
int i = 5;

int *const r = &i;

*r = 7;
}

Would this be a correct way of thinking about it? Yes, I suppose. Then
again, there are other ways of achieving the objective. For all you know,
the compiler might just look at the definition of "r" and think, "Hmm... r
is just i", and change it to:

int main()
{
int i = 5;

i = 7;
}

It might even achieve it internally something like:

int main()
{
int i = 5;

#define r i

r = 7;

#undef r
}

Who knows? All that matters is that the implementation accurately achieves
the behaviour of references.

References are most useful when passing objects, and returning objects,
from functions:

void Func(int &i)
{
i = 5;
}

How does the compiler compile this? Well, the C++ Standard doesn''t say how.
In a way, I think of references as magical little things that just get the
job done.

However, as a person who''s familiar with how computers actually work, I
know that there must be some sort of indirection involved (i.e. pointers)
if the function is not inline. Therefore, I would presume that the compiler
does it something like:

void __Func(int *const p)
{
*p = 5;
}

#define Func(i) __Func(&(i))

Then again, the compiler might have some new-fangled way of getting this
done, who knows?! All that''s important is that the job gets done.

Also, with returning objects by reference:

int &Func()
{
static int i;

return i;
}

int main()
{
Func() = 7;
}

How does the compiler make this work? Well, if there''s no function inlining
involved, then I''d presume it does something like:

int *Func()
{
static int i;

return &i;
}

int main()
{
*Func() = 7;
}

This would be one way for the compiler to achieve its objective. The fact
of the matter though is that references are little puffs of pixie dust.
They don''t make sense, and they can''t be explained. They just do what they
do.

References are special in one way though. When you initialise a reference
with an R-value, something special happens:

int &r = 5; /* Won''t compile */

but:

int const &r = 5; /* No problem */

You might think this is strange -- how could it possibly work? How could it
be equivalent to something like:

int const *const p = &(5);

The answer is simple: It isn''t. A "reference to const" is special if it is
initialised with an R-value. The original line, int &r = 5;, is treated as
if you wrote:

int const __literal = 5;
int const &r = __literal;

Now, you can see that the "temporary" object has the same lifetime as the
reference.

One more thing I''ll mention before I go... someone was interested by the
following code:

struct MyStruct {
int &a;
double &b;
};

#include <ostream>
#include <iostream>

int main()
{
int i; double d;

MyStruct ms = {i,d};

ms.a = 5;

std::cout << sizeof ms << std::endl;
}

The person was interested as to why "ms" had a particular size. Answer: The
C++ Standard doesn''t care. In reality though, the type, "MyStruct", must
achieve its objective. With a knowledge of how computers work, I can see
that the handiest way to achieve this particular objective would probably
be to use pointers internally:

struct MyStruct {
int *const a;
double *const b;
};

#include <ostream>
#include <iostream>

int main()
{
int i; double d;

MyStruct ms = {&i,&d};

*ms.a = 5;

std::cout << sizeof ms << std::endl;
}

This would be one way for the compiler to achieve the behaviour of
"MyStruct". But again, it doesn''t have to do it this way.

References are magical pixie dust as the end of the day, they have no
foundation in actual computer science -- they just do what they do.

--

Frederick Gotham

推荐答案

Frederick Gotham:
Frederick Gotham:

原始行,int& r = 5;,是

就像你写的那样对待:


int const __literal = 5;

int const& r = __literal;
The original line, int &r = 5;, is
treated as if you wrote:

int const __literal = 5;
int const &r = __literal;



意思是写:


原始行,int const& r = 5;


-


Frederick Gotham


Meant to write:

The original line, int const &r = 5;

--

Frederick Gotham




Frederick Gotham写道:

Frederick Gotham wrote:

当我是C ++的初学者时,我对引用的想法很挣扎。

先学会了如何使用指针,我犹豫不决接受

引用只是做他们的工作就是这样。
When I was a beginner in C++, I struggled with the idea of references.
Having learned how to use pointers first, I was hesitant to accept that
references just "do their job" and that''s it.



我认为像const引用一样是部分惯例。当

传递(或返回)参考时你会说有一个对象

附加到这个..别担心它。当你传递一个指针时,你会说你最好检查一下是否附有一个物体。


const类似。在两种情况下,如果你觉得有必要证明它不是绝对正确的,那么

惯例当然是可能的,那么

对它没有法律,但当你得到它代码做什么

它在锡上说是有用的和使用的和代码不起来。


问候

Andy Little

I think that like const references are partly a convention. When
passing (or returning) a reference you are saying there is an object
attached to this.. don''t worry about it. When passing a pointer you are
saying you had better check there Is an object attached to this.

const is similar. It is of course possible in both cases to knacker the
convention if you feel the need to prove that its not infallible, there
is no law against it, but when you get down to it code that does what
it says on the tin is useful and used and code that doesnt aint.

regards
Andy Little




kwikius写道:

kwikius wrote:

Frederick Gotham写道:
Frederick Gotham wrote:

当我是C ++的初学者时,我很挣扎于引用的想法。

学会了如何使用指针首先,我犹豫是否接受

引用的只是做他们的工作。就是这样。
When I was a beginner in C++, I struggled with the idea of references.
Having learned how to use pointers first, I was hesitant to accept that
references just "do their job" and that''s it.



我认为像const引用一样是部分惯例。当

传递(或返回)参考时你会说有一个对象

附加到这个..别担心它。当你传递一个指针时,你会说你最好检查一下是否附有一个物体。


const类似。在两种情况下,如果你觉得有必要证明它不是绝对正确的,那么

惯例当然是可能的,那么

对它没有法律,但当你得到它代码做什么

它在锡上说是有用的和使用的代码,并没有。


I think that like const references are partly a convention. When
passing (or returning) a reference you are saying there is an object
attached to this.. don''t worry about it. When passing a pointer you are
saying you had better check there Is an object attached to this.

const is similar. It is of course possible in both cases to knacker the
convention if you feel the need to prove that its not infallible, there
is no law against it, but when you get down to it code that does what
it says on the tin is useful and used and code that doesnt aint.



Oh aye和编译器似乎在优化参考和

inling函数方面比使用指针做得更好,也许他们只是更喜欢&符号/>
到星号...


问候

Andy Little

Oh aye and compilers seem to do a better job optimising references and
inling functions than with pointers, maybe they just prefer ampersands
to asterisks...

regards
Andy Little


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

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