广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++中引用的示例分析
  • 949
分享到

C++中引用的示例分析

2023-06-15 10:06:04 949人浏览 独家记忆
摘要

这篇文章将为大家详细讲解有关c++中引用的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。背景在C/C++中,访问一个变量只能通过两种方式被访问,传递,或者查询。这两种方式是:通过值访问/传递变量通

这篇文章将为大家详细讲解有关c++中引用的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

背景

C/C++中,访问一个变量只能通过两种方式被访问,传递,或者查询。这两种方式是:

通过值访问/传递变量

通过地址访问/传递变量–这种方法就是指针

除此之外没有第三种访问和传递变量值的方法。引用变量也就是个指针变量,它也拥有内存空间。最关键的是引用是一种会被编译器自动解引用的指针。很难相信么?

下面是一段使用引用的简单c++代码

#include <iOStream.h>  int main()  {      int i = 10;   // A simple integer variable      int &j = i;   // A Reference to the variable i      j++;   // Incrementing j will increment both i and j.      // check by printing values of i and j      cout<<  i  <<  j  <<endl; // should print 11 11      // Now try to print the address of both variables i and j      cout<<  &i  <<  &j  <<endl;      // surprisingly both print the same address and make us feel that they are      // alias to the same memory location.      // In example below we will see what is the reality      return 0;  }

引用其实就是c++中的常量指针。表达式int &i = j;将会被编译器转化成int *const i = &j;而引用之所以要初始化是因为const类型变量必须初始化,这个指针也必须有所指。下面我们再次聚焦到上面这段代码,并使用编译器的那套语法将引用替换掉。

#include <iostream.h>  int main()  {      int i = 10;            // A simple integer variable      int *const j = &i;     // A Reference to the variable i      (*j)++;                // Incrementing j. Since reference variables are                             // automatically dereferenced by compiler      // check by printing values of i and j      cout<<  i  <<  *j  <<endl; // should print 11 11      // A * is appended before j because it used to be reference variable      // and it should get automatically dereferenced.      return 0;  }

读者一定很奇怪为什么我上面这段代码会跳过打印地址这步。这里需要一些解释。因为引用变量时会被编译器自动解引用的,那么一个诸如cout << &j << endl;的语句,编译器就会将其转化成语句cout << &*j << endl;现在&*会相互抵消,这句话变的毫无意义,而cout打印的j值就是i的地址,因为其定义语句为int *const j = &i;

所以语句cout << &i << &j << endl;变成了cout << &i << &*j << endl;这两种情况都是打印输出i的地址。这就是当我们打印普通变量和引用变量的时候会输出相同地址的原因。

下面给出一段复杂一些的代码,来看看引用在级联(cascading)中是如何运作的。

#include <iostream.h>  int main()  {      int i = 10; // A Simple Integer variable      int &j = i; // A Reference to the variable      // Now we can also create a reference to reference variable.       int &k = j; // A reference to a reference variable      // Similarly we can also create another reference to the reference variable k      int &l = k; // A reference to a reference to a reference variable.      // Now if we increment any one of them the effect will be visible on all the      // variables.      // First print original values      // The print should be 10,10,10,10      cout<<  i  <<  ","  <<  j  <<  ","  <<  k  <<  ","  <<  l  <<endl;      // increment variable j      j++;       // The print should be 11,11,11,11      cout<<  i  <<  ","  <<  j  <<  ","  <<  k  <<  ","  <<  l  <<endl;      // increment variable k      k++;      // The print should be 12,12,12,12      cout<<  i  <<  ","  <<  j  <<  ","  <<  k  <<  ","  <<  l  <<endl;      // increment variable l      l++;      // The print should be 13,13,13,13      cout<<  i  <<  ","  <<  j  <<  ","  <<  k  <<  ","  <<  l  <<endl;      return 0;  }

下面这段代码是将上面代码中的引用替换之后代码,也就是说明我们不依赖编译器的自动替换功能,手动进行替换也能达到相同的目标。

#include <iostream.h>  int main()  {      int i = 10;         // A Simple Integer variable      int *const j = &i;     // A Reference to the variable      // The variable j will hold the address of i      // Now we can also create a reference to reference variable.       int *const k = &*j;     // A reference to a reference variable      // The variable k will also hold the address of i because j       // is a reference variable and       // it gets auto dereferenced. After & and * cancels each other       // k will hold the value of      // j which it nothing but address of i      // Similarly we can also create another reference to the reference variable k      int *const l = &*k;     // A reference to a reference to a reference variable.      // The variable l will also hold address of i because k holds address of i after      // & and * cancels each other.      // so we have seen that all the reference variable will actually holds the same      // variable address.      // Now if we increment any one of them the effect will be visible on all the      // variables.      // First print original values. The reference variables will have * prefixed because       // these variables gets automatically dereferenced.      // The print should be 10,10,10,10      cout<<  i  <<  ","  <<  *j  <<  ","  <<  *k  <<  ","  <<  *l  <<endl;      // increment variable j      (*j)++;       // The print should be 11,11,11,11      cout<<  i  <<  ","  <<  *j  <<  ","  <<  *k  <<  ","  <<  *l  <<endl;      // increment variable k      (*k)++;      // The print should be 12,12,12,12      cout<<  i  <<  ","  <<  *j  <<  ","  <<  *k  <<  ","  <<  *l  <<endl;      // increment variable l      (*l)++;      // The print should be 13,13,13,13      cout  <<  i  <<  ","  <<  *j  <<  ","  <<  *k  <<  ","  <<  *l  <<endl;      return 0;  }

我们通过下面代码可以证明c++的引用不是神马别名,它也会占用内存空间的。

#include <iostream.h>  class Test  {      int &i;   // int *const i;      int &j;   // int *const j;      int &k;   // int *const k;   };  int main()  {          // This will print 12 i.e. size of 3 pointers      cout<<  "size of class Test = "  <<   sizeof(class Test)  <<endl;      return 0;  }

结论

我希望这篇文章能把c++引用的所有东东都解释清楚,然而我要指出的是c++标准并没有解释编译器如何实现引用的行为。所以实现取决于编译器,而大多数情况下就是将引用实现为一个const指针。

引用支持c++虚函数机制的代码

#include <iostream.h>  class A  {  public:           virtual void print() { cout<<"A.."<<endl; }  };  class B : public A  {  public:           virtual void print() { cout<<"B.."<<endl; }  };     class C : public B  {  public:           virtual void print() { cout<<"C.."<<endl; }  };  int main()  {           C c1;           A &a1 = c1;           a1.print(); // prints C           A a2 = c1;           a2.print(); // prints A           return 0;  }

关于“C++中引用的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

--结束END--

本文标题: C++中引用的示例分析

本文链接: https://www.lsjlt.com/news/279681.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • c语言怎么计算字符串长度
    在C语言中,可以使用内置的strlen()函数来计算字符串的长度。该函数位于string.h头文件中。以下是一个示例代码: #inc...
    99+
    2024-04-30
    c语言
  • c语言怎么判断三个数的大小
    可以通过比较三个数之间的大小关系来判断它们的大小。以下是一个示例代码: #include <stdio.h> int ...
    99+
    2024-04-30
    c语言
  • c++排序函数sort怎么使用
    在C++中,使用sort函数对容器中的元素进行排序,可以按照默认的升序排列,也可以自定义排序规则。 下面是一个简单的例子,对一个ve...
    99+
    2024-04-30
    c++
  • c++中string的compare函数怎么使用
    在C++中,可以使用string的compare函数来比较两个string对象的大小。该函数的原型如下: int compare(c...
    99+
    2024-04-30
    c++
  • c++中string的compare函数有什么用
    c++中string的compare函数用于比较两个字符串的大小关系。该函数返回一个整数值,表示两个字符串的比较结果。 具体来说,如...
    99+
    2024-04-30
    c++
  • c++中print函数的功能有哪些
    在C++中,通常使用std::cout来进行输出操作,而不是使用print函数。std::cout是C++标准库中的一个输出流对象,...
    99+
    2024-04-30
    c++
  • c语言数据结构怎么使用
    在C语言中,数据结构可以通过定义结构体来实现。以下是一些常见的数据结构的使用方法示例: 链表: #include <std...
    99+
    2024-04-30
    c语言
  • c++中print函数的用法是什么
    在C++中没有内置的print函数,但可以使用cout来输出内容到控制台。cout是C++标准库中的一个输出流对象,可以使用<...
    99+
    2024-04-30
    c++
  • c++中concept的用法是什么
    在C++20中,Concept是一种新的语言特性,用于定义类型要求和约束。Concept可以被用来约束函数模板、类模板和普通函数的参...
    99+
    2024-04-30
    c++
  • c++中concept的作用是什么
    在C++中,concept的作用是定义一种通用的约束,用于限制模板参数的类型范围。通过使用concept,可以在编译时对模板参数进行...
    99+
    2024-04-30
    c++
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作