c&c++语言参考手册_C ++中的参考

c&c++语言参考手册

References are like constant pointers that are automatically dereferenced. It is a new name given to an existing storage. So when you are accessing the reference, you are actually accessing that storage.

引用就像自动取消引用的常量指针一样。 它是现有存储的新名称。 因此,当您访问参考时,实际上是在访问该存储。

int main()
{ 
    int y=10;
    int &r = y;  // r is a reference to int y
    cout << r;
}

10

10

There is no need to use the * to dereference a reference variable.

无需使用*取消引用引用变量。

引用和指针之间的区别 (Difference between Reference and Pointer)

ReferencesPointers
Reference must be initialized when it is created.Pointers can be initialized any time.
Once initialized, we cannot reinitialize a reference.Pointers can be reinitialized any number of time.
You can never have a NULL reference.Pointers can be NULL.
Reference is automatically dereferenced.* is used to dereference a pointer.
参考资料 指针
创建引用时必须对其进行初始化。 指针可以随时初始化。
初始化后,我们将无法重新初始化引用。 指针可以任意次数重新初始化。
您永远不会有NULL引用。 指针可以为NULL。
引用将自动取消引用。 *用于取消引用指针。

功能参考 (References in Funtions)

References are generally used for function argument lists and function return values, just like pointers.

引用通常用于函数参数列表和函数返回值,就像指针一样。

在函数中使用引用的规则 (Rules for using Reference in Functions)

  1. When we use reference in argument list, we must keep in mind that any change to the reference inside the function will cause change to the original argument outside th function.

    当在参数列表中使用引用时,必须记住,对函数内部引用的任何更改都将导致对函数外部原始参数的更改。

  2. When we return a reference from a function, you should see that whatever the reference is connected to shouldn't go out of scope when fnction ends. Either make that global or static

    当我们从一个函数返回一个引用时,您应该看到,当函数结束时,该引用所连接的任何内容都不会超出范围。 使全局静态

解释引用用法的示例 (Example to explain the use of References)

Below we have a simple code example to explain the use of references in C++,

下面我们有一个简单的代码示例来说明C ++中引用的使用,

int* first (int* x)
{ 
    (*x++);
    return x;   // SAFE, x is outside this scope
}

int& second (int& x)
{ 
    x++;
    return x;   // SAFE, x is outside this scope
}

int& third ()
{ 
    int q;
    return q;   // ERROR, scope of q ends here
}

int& fourth ()
{ 
    static int x;
    return x;   // SAFE, x is static, hence lives till the end.
}

int main()
{
    int a=0;
    first(&a);   // UGLY and explicit
    second(a);   // CLEAN and hidden
}

We have four different functions in the above program.

在上面的程序中,我们有四个不同的功能。

  • first() takes a pointer as argument and returns a pointer, it will work fine. The returning pointer points to variable declared outside first(), hence it will be valid even after the first() ends.

    first()将指针作为参数并返回一个指针,它将正常工作。 返回的指针指向在first()外部声明的变量,因此即使在first()结束之后它也将有效。



  • Similarly, second() will also work fine. The returning reference is connected to valid storage, that is int a in this case.

    同样, second()也可以正常工作。 返回的引用连接到有效存储,在这种情况下为int a



  • But in case of third(), we declare a variable q inside the function and try to return a reference connected to it. But as soon as function third() ends, the local variable q is destroyed, hence nothing is returned.

    但是在third()的情况下,我们在函数内部声明了一个变量q并尝试返回与其连接的引用。 但是一旦函数third()结束,局部变量q就被破坏,因此什么也不会返回。



  • To remedify above problem, we make x as static in function fourth(), giving it a lifetime till main() ends, hence now a reference connected to x will be valid when returned.

    为了解决上述问题,我们在函数four ()中将x设为静态 ,赋予它一个生存期,直到main()结束,因此现在连接到x的引用在返回时将是有效的。

C ++中的const参考 (Const Reference in C++)

Const reference is used in function arguments to prevent the function from changing the argument.

函数参数中使用了常量引用,以防止函数更改参数。

void g(const int& x)
{ 
    x++; 
}   // ERROR

int main()
{
    int i=10;
    g(i);
}

We cannot change the argument in the function because it is passed as const reference.

我们不能在函数中更改参数,因为它是作为const引用传递的。

参数传递准则 (Argument Passing Guidelines)

Usually call by value is used during funtcion call just to save our object or variable from being changed or modified, but whenever we pass an argument by value, its new copy is created. If we pass an object as argument then a copy of that object is created (constructor and destructors called), which affects efficiency.

通常在函数调用期间使用按值调用只是为了防止更改或修改我们的对象或变量,但是每当我们按值传递参数时,都会创建其新副本。 如果我们将一个对象作为参数传递,则会创建该对象的副本(调用构造函数和析构函数),这会影响效率。

Hence, we must use const reference type arguments. When we use, const reference, only an address is passed on stack, which is used inside the function and the function cannot change our argument because it is of const type.

因此,我们必须使用const引用类型参数。 当我们使用const引用时,仅在堆栈上传递一个地址,该地址在函数内部使用,并且该函数不能更改我们的参数,因为它是const类型的。

So using const reference type argument reduces overhead and also saves our argument from being changed.

因此,使用const引用类型参数可以减少开销,还可以避免参数被更改。

翻译自: https://www.studytonight.com/cpp/references-in-cpp.php

c&c++语言参考手册

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值