C语言中的引用是什么?
我刚刚开始使用C++,遇到过一些参考资料,但还没有完全理解。
正如我读到的,引用是对象的替代名称。为什么要使用它而不是直接访问对象,因为对引用的任何操作都会直接反映在对象上......?
- 为什么以及何时使用它们?
- ist 是否像一个常量指针,每次使用时都会被引用......?
而且,它说
double& dr = 1; ---- says it is an error (some lavalue needed)
const double& cdr = 1; ---- says it is ok.
我没有正确理解它。所以请解释为什么会这样...
谢谢...:)
I have just started C++ and have come across references and have not understood completely.
References , as i read is an alternative name for an object.Why use that instead of directly accessing the object as any operation on references is directly reflected on the object ...?
- Why and when are they used ?
- Is ist like a constant pointer that is referenced each time it is used ... ?
And , it says
double& dr = 1; ---- says it is an error (some lavalue needed)
const double& cdr = 1; ---- says it is ok.
i dont understand it properly..So please explain why it is so ...
Thank You...:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
传递对函数的引用,然后让函数使用该引用几乎就像将指针传递给函数,然后让函数取消引用该指针一样。在许多情况下,机器代码实现是相同的。不过,还是存在一些差异,尤其是在内联扩展的函数的情况下。如果变量通过引用传递给内联函数,则编译器在扩展函数时通常能够替换变量本身(即使存储在机器寄存器中)。相比之下,如果获取变量的地址并将其作为指向函数的指针传递,然后取消引用它,则编译器不太可能找出该优化,除非它不仅确定这一点 - 至少对于一个特定的扩展函数——指针将始终指向该变量,但指针不会在其他任何地方使用(如果指针在其他地方使用,则该变量不能保存在寄存器中)。
Passing a reference to a function and then having the function use the reference is almost like passing a pointer to the function and then having the function dereference the pointer. In many cases, the machine-code implementation will be identical. There are some differences, though, especially in the case of functions that get expanded inline. If a variable is passed by reference to an inline function, the compiler will often be able to substitute the variable itself--even if stored in a machine register--when expanding the function. By contrast, if one takes the address of a variable and passes that as a pointer to a function which then dereferences it, the compiler is less likely to figure out that optimization unless it determines not only that--at least for one particular expansion of the function--the pointer will always point to that variable, but also that the pointer will not be used anywhere else (if the pointer was used elsewhere, the variable could not be kept in a register).
引用的实用性在将参数传递给函数的上下文中最为明显。
即,
int a;
func 定义: void foo (int¶m) {param = 1;}
func 调用: foo(a);
“param”别名“a”的方式很干净,其意图很容易被该代码的读者以及编译器理解,当内联引用所需的任何额外内存分配时,编译器可能会进行优化。
Utility of references is most visible in the context of passing parameters to functions.
I.e,
int a;
func definition: void foo (int& param) {param = 1;}
func call: foo(a);
The way as 'param' aliases 'a' is clean and its intention is easily understood by a reader of this code as well as compiler that may optimize away when inlining any additional memory allocation needed for the reference.
无效,因为
1
被视为const double
类型,因此如果您想要引用该变量,则需要引用const double
代码>所以是正确的。
Is invalid because
1
is viewed to be of typeconst double
so if you want a reference to that variable you need to have a reference to aconst double
soIs correct.
引用是代表它们引用的另一个对象的语言实体。非常量引用是左值,必须使用左值进行初始化。它们的用途如下:
当用作函数参数时,它们告诉调用者必须传递一个可能由函数写入的左值:
另一方面,常量引用可以绑定到右值。在函数参数中,它们通常用于避免过多的复制:
请注意,这可能会也可能不会产生更快的代码。
当在普通代码中使用 const 引用时,它们也可以绑定右值,并且 作为一条特殊规则,它们延长了所绑定对象的生命周期。这就是您在代码中看到的:
所有引用都是多态的,就像指针一样:
并且所有引用都是别名,就像指针一样:
References are language entitities that represent another object they refer to. Nonconst references are lvalues, and must be initialized with an lvalue. They can be useful like this:
When used as a function parameter, they tell the caller he has to pass an lvalue that might be written to by the function:
Const references, on the other hand, can be bound to rvalues. In function parameters, they are usually used to avoid excessive copies:
Note that this may or may not yield faster code.
When const-references are used in normal code, they can bind rvalues, too, and as a special rule, they extend the lifetime of the object they are bound to. This is what you saw in your code:
All references are polymorphic, like pointers:
and all references are aliases like pointers:
仔细阅读维基百科文章。总而言之,引用是指针的更友好版本,通常用于将对象作为引用传递到函数中,而不必担心空指针。
解释这个例子:
将数字
1
想象为一个变量。编译时,该数字被放入内存的全局部分,程序可以引用它,但不修改。所以它的类型是: const int
double &dr = 1
正在尝试分配dr
(对 double<的 引用/strong>) 到const int 1
。由于1
是一个常量,编译器不允许您对其进行非常量引用。在第二行中:
const double &dr = 1
尝试将dr
(对 double 的常量引用)分配给常量 int 1
。这是可行的,因为引用也是const,因此可以指向const int
。编辑
const int
在分配之前会转换为const double
。Give the wikipedia article a good read through. To sum it up, references are more friendly version of pointers which are commonly used to pass objects as references into functions without worrying about a null pointer.
To explain the example:
Think of the number
1
represented as a variable. When compiled, this number is put into the global section of the memory which can be referenced by the program, but not modified.So it is of type: const int
double &dr = 1
is trying to assigndr
(a reference to a double) to theconst int 1
. Since1
is a constant, the compiler will not allow you to make a non-constant reference to it.In the second line:
const double &dr = 1
is trying to assigndr
(a constant reference to a double) theconst int 1
. This works because the reference is also const and therefore can point to aconst int
.EDIT
The
const int
is converted to aconst double
before assigned.引用改进了语法,因此不需要指针取消引用。
假设 Base 是一个可能派生自的类:
引用就像常量指针(不是指向常量的指针 - 即您可以更改对象,但不能更改您所指向的内容)。 const 引用是一个引用,通过它你可以做 const 对象可以做的事情。
引用也很好,因为不能有空引用
References improve the syntax, so no pointer dereference needed.
Assuming Base is a class that may be derived from:
References are like constant pointers (NOT pointers to constants - i.e. you can change the object, but you can't change to what you're pointing). const reference is a reference through which you can do things that can be done on const object.
References are also good, because you can't have a null reference
我同意你的看法。仅使用引用作为别名并不是很有用。
如果您将其视为不可变指针,则会更有用。但实际上没那么有用。
实际上,它用于定义干净的接口。例如,当您定义:
您说 param 是 foo 中的只读参数。
不要忘记您必须为引用分配一个值。
的信息,请参阅 C++ faqlite
有关更多my2c
I agree with you. using references as just an alias name is not very useful.
It is more useful if you consider it as an immutable pointer. But not that useful in fact.
Practically, it is used to define clean interfaces. For example when you define:
You say that param is a read-only parameter in foo.
Do not forget that you MUST assign a value to a reference.
See the C++ faqlite on references for more
my2c
引用基本上是一个看起来像对象的指针。尽管您可以费尽心思创建一个 NULL 引用,但获得 NULL 引用是非常非常困难的。
对于您的示例,1 是右值或结果。它只是一个临时变量,无法修改。因此你不能对它进行非常量引用。但是您可以对其进行常量引用。这意味着您无法更改引用的值。
下面是创建 NULL 引用的示例。不要这样做!
A reference is basically a pointer that looks like an object. It is very very hard to get a NULL reference though you can go through hoops and create one.
With regards to your example, 1 is an rvalue or a result. It is just a temporary variable and can not be modified. Thus you can't take a non const reference to it. However you can take a const reference to it. This means you can't change the value of the reference.
Here is an example of creating a NULL reference. Don't do it!
引用是最初在 C 代码中的另一种方式,就像这样
,在 C++ 中的引用则像这样 这
是一种使用按引用调用参数而不是使用 C 星号表示法的更简洁的语法方式/star 表示指针并作为按引用调用参数...以及直接在函数外部修改参数...
看看 Bjarne Stoustrap 的此处页面介绍了 C++ 的原理以及技术常见问题解答< a href="http://www2.research.att.com/~bs/bs_faq2.html" rel="nofollow noreferrer">此处
References are another way of what was originally in C code like this
with references in C++ it would be like this
It's a neater syntactical way of using a call-by-reference parameter instead of using the C's notation asterisk/star to indicate a pointer and as a call-by-reference parameter...and modifying the parameter directly outside of the function...
Have a look at Bjarne Stoustrap's page here which covers how C++ is and also here on the technical faq here
C++ 按值传递参数,这意味着如果您有一个函数,例如:
默认情况下,C++ 将复制
MyObject
,不会直接使用传入的对象。所以,引用的一种用途是确保您正在处理同一个对象:或者,如果您不修改
o
:C++ passes parameters by value, meaning if you have a function such as:
By default C++ will make a copy of a
MyObject
, not directly use the object being passed in. So, one use of references is to ensure you are working on the same object:Or, if you aren't modifying
o
: