为什么需要在C ++中引用? [英] Why is there a need for reference in C++?

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

问题描述



我一直在使用C ++。我并不完全清楚使用C ++中的引用概念。


- 为什么需要引入一个名为Reference的概念。在

C ++当一切都正常使用普通指针时?


- 有什么引用可以做指针不能吗???


请澄清,

Sarathy

Hi,
I have been using C++ for a while. I am not entirely clear with
the concepts of reference in C++.

- Why was there a need for introducing a concept called "Reference" in
C++ when everything was working fine with normal pointers?

- Is there anything that a reference can do that a pointer cannot ???

Please clarify,
Sarathy

推荐答案

2006年7月29日11:39:53 -0700,sarathy < sp ********* @ gmail.com>

写道:
On 29 Jul 2006 11:39:53 -0700, "sarathy" <sp*********@gmail.com>
wrote:

>

我一直在使用C ++。我并不完全清楚使用C ++中的引用概念。

- 为什么需要引入一个名为Reference的概念。在使用普通指针一切正常的时候用C ++做什么?

- 有什么引用可以做指针不能吗???

请澄清,
Sarathy
>Hi,
I have been using C++ for a while. I am not entirely clear with
the concepts of reference in C++.

- Why was there a need for introducing a concept called "Reference" in
C++ when everything was working fine with normal pointers?

- Is there anything that a reference can do that a pointer cannot ???

Please clarify,
Sarathy



指针引起过度摩擦,据信是造成航天器爆炸的原因。参考文献是为安全而发明的。

在太空车上使用。

Pointers cause excess friction, and are believed to have been the
cause of explosions in space craft. References were invented for safe
use aboard spacebound vehicles.


sarathy写道:
sarathy wrote:



我一直在使用C ++。我并不完全清楚使用C ++中的引用概念。


- 为什么需要引入一个名为Reference的概念。在

C ++什么时候一切正常与指针一起工作?
Hi,
I have been using C++ for a while. I am not entirely clear with
the concepts of reference in C++.

- Why was there a need for introducing a concept called "Reference" in
C++ when everything was working fine with normal pointers?



这不好,是吗?指针*很棘手。现在,在C ++中,

指针的棘手问题因例外情况而变得复杂:因为异常可以在几乎任何点向未知位置转移控制流量,基本

一个指针的要求,每个new()都与delete()相匹配

每个执行路径都难以匹配。因此,创建了一个设备来消除某些指针的使用。参考和标准容器在此类别中为
。其他设备,如auto_ptr,被用来减轻其余指针使用情况的危险。

It wasn''t fine, was it? Pointers *are* tricky. Now, in C++ the trickyness of
pointers is compounded by exceptions: since exceptions can divert the flow
of control at almost any point and toward unknown locations, the basic
requirement of a pointer that every new() is matched by a delete() along
each path of execution is harder to match. Thus, a device was created to
eliminate some uses of pointers. References and standard containers are in
this category. Other devices, like auto_ptr, were introduced to mitigate
the dangers for the remaining cases of pointer use.


- 是有什么引用可以做一个指针不能???
- Is there anything that a reference can do that a pointer cannot ???



参考可以延长临时工的使用寿命:


#include< iostream>


struct log {


log(void){

std :: cout<< "施工" << std :: endl;

}


log(log const&){

std :: cout<< "复印" << std :: endl;

}


~log(无效){

std :: cout<< "破坏" << std :: endl;

}


void access(void)const {

std :: cout<< "接入" << std :: endl;

}


};


log create_tmp(void){

return(log());

}


int main(void){

{

log const& ref = create_tmp();

ref.access();

}

std :: cout<< std :: endl;

{

//警告:UB

log const * ptr =& create_tmp();

ptr-> access();

}

}


//文件结尾

Best


Kai-Uwe Bux

References can extend the life-time of temporaries:

#include <iostream>

struct log {

log ( void ) {
std::cout << "construction" << std::endl;
}

log ( log const & ) {
std::cout << "copy" << std::endl;
}

~log ( void ) {
std::cout << "destruction" << std::endl;
}

void access ( void ) const {
std::cout << "access" << std::endl;
}

};

log create_tmp ( void ) {
return ( log() );
}

int main ( void ) {
{
log const & ref = create_tmp();
ref.access();
}
std::cout << std::endl;
{
// warning: UB
log const * ptr = &create_tmp();
ptr->access();
}
}

// end of file
Best

Kai-Uwe Bux


文章< 11 ******** **************@75g2000cwc.googlegroups。 com>,

" sarathy" < sp ********* @ gmail.comwrote:
In article <11**********************@75g2000cwc.googlegroups. com>,
"sarathy" <sp*********@gmail.comwrote:



我一直在使用C ++而。我并不完全清楚使用C ++中的引用概念。


- 为什么需要引入一个名为Reference的概念。在

C ++什么时候一切正常与指针一起工作?
Hi,
I have been using C++ for a while. I am not entirely clear with
the concepts of reference in C++.

- Why was there a need for introducing a concept called "Reference" in
C++ when everything was working fine with normal pointers?



我读到C ++的设计和演变很久以前,Bjarne Stroustrup很长一段时间,所以我可能记不起来了,但是我记得它已经用了b / b
引用语言来使操作员超载工作正常。

I read "The Design and Evolution of C++" by Bjarne Stroustrup a long
time ago so I may not remember this correctly, however as I remember it
references were put in the language to make operator overload work right.


- 引用可以做什么,指针不能???
- Is there anything that a reference can do that a pointer cannot ???



class MyClass {};


MyClass运算符+(const MyClass& lhs,const MyClass& rhs);


如果没有引用,你唯一的选择就是传递价值(对于一个大类的对象来说,这可能是非常昂贵的,或者是通过指针传递的

会使调用代码看起来像:

MyClass a,b;

MyClass c =& a +& b;


这看起来很笨拙。

class MyClass { };

MyClass operator+( const MyClass& lhs, const MyClass& rhs );

Without references, your only choices are to pass by value (which could
be quite expensive for objects of a big class, or pass by pointer which
would make the calling code look like:

MyClass a, b;
MyClass c = &a + &b;

Which seems rather clumsy.


这篇关于为什么需要在C ++中引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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