- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章解析C++引用由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
我选择写C++中的引用是因为我感觉大多数人误解了引用。而我之所以有这个感受是因为我主持过很多C++的面试,并且我很少从面试者中得到关于C++引用的正确答案.
那么c++中引用到底意味这什么呢?通常一个引用让人想到是一个引用的变量的别名,而我讨厌将c++中引用定义为变量的别名。这篇文章中,我将尽量解释清楚,c++中根本就没有什么叫做别名的东东.
在c/c++中,访问一个变量只能通过两种方式被访问,传递,或者查询。这两种方式是:
1.通过值访问/传递变量 。
2.通过地址访问/传递变量–这种方法就是指针 。
除此之外没有第三种访问和传递变量值的方法。引用变量也就是个指针变量,它也拥有内存空间。最关键的是引用是一种会被编译器自动解引用的指针。很难相信么?
下面是一段使用引用的简单c++代码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#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类型变量必须初始化,这个指针也必须有所指。下面我们再次聚焦到上面这段代码,并使用编译器的那套语法将引用替换掉.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#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)中是如何运作的.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#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;
}
|
下面这段代码是将上面代码中的引用替换之后代码,也就是说明我们不依赖编译器的自动替换功能,手动进行替换也能达到相同的目标.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#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++的引用不是神马别名,它也会占用内存空间的.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#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++虚函数机制的代码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#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;
}
|
上述代码使用引用支持虚函数机制。如果引用仅仅是一个别名,那如何实现虚函数机制,而虚函数机制所需要的动态信息只能通过指针才能实现,所以更加说明引用其实就是一个const指针.
以上就是解析C++引用的详细内容,更多关于C++引用的资料请关注我其它相关文章! 。
原文链接:https://www.cnblogs.com/lsgxeva/p/7785261.html 。
最后此篇关于解析C++引用的文章就讲到这里了,如果你想了解更多关于解析C++引用的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我一直在使用 AJAX 从我正在创建的网络服务中解析 JSON 数组时遇到问题。我的前端是一个简单的 ajax 和 jquery 组合,用于显示从我正在创建的网络服务返回的结果。 尽管知道我的数据库查
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我在尝试运行 Android 应用程序时遇到问题并收到以下错误 java.lang.NoClassDefFoundError: com.parse.Parse 当我尝试运行该应用时。 最佳答案 在这
有什么办法可以防止etree在解析HTML内容时解析HTML实体吗? html = etree.HTML('&') html.find('.//body').text 这给了我 '&' 但我想
我有一个有点疯狂的例子,但对于那些 JavaScript 函数作用域专家来说,它看起来是一个很好的练习: (function (global) { // our module number one
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我需要编写一个脚本来获取链接并解析链接页面的 HTML 以提取标题和其他一些数据,例如可能是简短的描述,就像您链接到 Facebook 上的内容一样。 当用户向站点添加链接时将调用它,因此在客户端启动
在 VS Code 中本地开发时,包解析为 C:/Users//AppData/Local/Microsoft/TypeScript/3.5/node_modules/@types//index而不是
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我被赋予了将一种语言“翻译”成另一种语言的工作。对于使用正则表达式的简单逐行方法来说,源代码过于灵活(复杂)。我在哪里可以了解更多关于词法分析和解析器的信息? 最佳答案 如果你想对这个主题产生“情绪化
您好,我在解析此文本时遇到问题 { { { {[system1];1;1;0.612509325}; {[system2];1;
我正在为 adobe after effects 在 extendscript 中编写一些代码,最终变成了 javascript。 我有一个数组,我想只搜索单词“assemble”并返回整个 jc3_
我有这段代码: $(document).ready(function() { // }); 问题:FB_RequireFeatures block 外部的代码先于其内部的代码执行。因此 who
背景: netcore项目中有些服务是在通过中间件来通信的,比如orleans组件。它里面服务和客户端会指定网关和端口,我们只需要开放客户端给外界,服务端关闭端口。相当于去掉host,这样省掉了些
1.首先贴上我试验成功的代码 复制代码 代码如下: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
什么是 XML? XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。 你可以通过本站学习 X
【PHP代码】 复制代码 代码如下: $stmt = mssql_init('P__Global_Test', $conn) or die("initialize sto
在SQL查询分析器执行以下代码就可以了。 复制代码代码如下: declare @t varchar(255),@c varchar(255) declare table_cursor curs
前言 最近练习了一些前端算法题,现在做个总结,以下题目都是个人写法,并不是标准答案,如有错误欢迎指出,有对某道题有新的想法的友友也可以在评论区发表想法,互相学习🤭 题目 题目一: 二维数组中的
我是一名优秀的程序员,十分优秀!