📄 thispointer.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>this 指标</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">C++
Gossip: this 指标</a></h1>
考虑下面这个类别:<br>
<ul>
<li>Ball.h</li>
</ul>
<pre>#include <string><br>using namespace std;<br><br>class Ball { <br>public: <br> Ball(); <br> Ball(double, const char*); <br> Ball(double, string&); <br> <br> double radius() {<br> return _radius;<br> }<br> <br> string& name() {<br> return _name; <br> }<br> <br> void radius(double radius) {<br> _radius = radius;<br> } <br> <br> void name(const char *name) {<br> _name = name;<br> }<br> <br> void name(string& name) {<br> _name = name;<br> }<br> <br> double volumn() {<br> return (4 / 3 * 3.14159 * _radius * _radius * _radius); <br> }<br> <br>private:<br> double _radius; // 半径 <br> string _name; // 名称 <br>};</pre>
<br>
当您使用Ball类别新增两个物件b1(1, "RBall")与b2(2,
"GBall")时,b1与b2会各自拥有自己的_radius与_name资料成员,然而函式成员却只有一份。 <br>
<br>
当您使用b1.name()方法取回_name的名称,它会传回"RBall"名称,而使用b2.name()时,它会传回"GBall"名称,即然类别
的函式成员只有一份,name()如何知道它传回的_name是b1物件的,还是b2物件的呢? <br>
<br>
其实您使用物件名称来呼叫函式成员时,程式会将物件记忆体位址告知函式成员,而在函式中,一个类别资料成员其实会隐含一个this指标,这个this指标
会储存传递进来的物件指标,当您呼叫name()函式时,其实相当于执行: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">string& name() {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
return this->_name; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>
</div>
<br>
b1呼叫name()方法时,this储存的就是b1的记忆体位址,而使用b2呼叫name()方法时,this储存的就是b2的记忆体位址,所以
name()总是可以正确的得知该传回哪一个物件的_name资料。 <br>
<br>
每一个类别的函式成员都会隐含一个this指标,用来指向呼叫它的物件,当您在函式中使用资料成员时,都会隐含的使用this指标,当然您也可以明确的指
定,例如在函式定义时使用: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">void name(string&
name) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
this-> _name = name;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>
</div>
<br>
这么作在这个例子中是多此一举,只是更明确的在程式码中撰写而已,然而这种写法在某些时机是需要的,例如参数名称与资料成员名称相同时,为了识别是参数或
是资料成员,您必须明确的使用this指标来指定,例如: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">void name(string&
name) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
this-> name = name; // 假设name是类别成员之一</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>
</div>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -