📄 memberfunctionptr.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>成员函式指标(Pointer to member function)</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: 成员函式指标(Pointer to member function)</a></h1>
在 <a href="FunctionPointer.html">函式指标</a>
中曾经介绍过如何宣告一个指向函式的指标,您也可以宣告一个指标指向物件的成员函式,这边仍然使用 <a href="ConstructorDestructor.html">建构函式、解构函式</a> 中的SafeArray类别来作说明。<br>
<br>
要宣告一个成员函式指标与单纯的宣告函式指标是类似的,所不同的是您要指定是哪一个类别的函式,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
(SafeArray::*mfPtr1)(int) = 0;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">void
(SafeArray::*mfPtr2)(int, int) = 0;</span><br>
</div>
<br>
上例中宣告了mfPtr1与mfPtr2两个成员函式指标,初始值设定为0,表示目前不指向任何成员函式,mfPtr1可以指向SafeArray具有
int返回值及一个int参数的成员函式(例如get()),而mfPtr2可以指向SafeArray中具有void及两个int参数的成员函式(例如
set())。<br>
<br>
上例中宣告成员函式指标的语法过于复杂,可以使用typedef加以简化,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">typedef void
(SafeArray::*MFPTR1)(int, int); </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">typedef int
(SafeArray::*MFPTR2)(int);</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">MFPTR1 mfPtr1 = 0;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">MFPTR2 mfPtr2 = 0;</span><br>
</div>
<br>
为了取得一个成员函式的位址值,您可以使用&取址运算子并指明是哪个类别的哪个成员函式,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">mfPtr1 =
&SafeArray::set;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">mfPtr2 =
&SafeArray::get;</span><br>
</div>
<br>
成员函式在记忆体中只会存在一份,在调用成员函式时,具体要配合实际的物件位址,物件位址会是 <a href="thisPointer.html">this
指标</a> 指向的位址,所以透过物件调用成员函式的方式如下:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">SafeArray safeArray(10);<br>
<br style="font-weight: bold;">
//
以下与safeArray.set(2, 10) 相同</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">(safeArray.*mfPtr1)(2,
10);</span><span style="font-weight: bold; font-style: italic;"><br>
<br>
</span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
// 以下的成员函式呼叫部份与safeArray.get(2)相同<br>
</span></div>
<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">cout <<
(safeArray.*mfPtr2)(2) << endl;</span></div>
<br>
"." 运算子之后使用*对指标取值,括号是给定引数。如果是物件指标的话,则可以如下:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">SafeArray *safePtr =
&safeArray;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">//
以下相当于safePtr->set(2, 100)</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">(safePtr->*mfPtr1)(2,
100);</span><br>
</div>
<br>
下例是个简单的运行程式,示范如何使用成员函式指标:<br>
<ul>
<li>main.cpp</li>
</ul>
<pre>#include <iostream><br>#include "SafeArray.h"<br>using namespace std;<br><br>int main() {<br> //int (SafeArray::*mfPtr1)(int) = 0;<br> //void (SafeArray::*mfPtr2)(int, int) = 0;<br> typedef void (SafeArray::*MFPTR1)(int, int); <br> typedef int (SafeArray::*MFPTR2)(int);<br><br> MFPTR1 mfPtr1 = 0;<br> MFPTR2 mfPtr2 = 0;<br> <br> mfPtr1 = &SafeArray::set;<br> mfPtr2 = &SafeArray::get;<br> <br> SafeArray safeArray(10);<br> <br> for(int i = 0; i < safeArray.length; i++) {<br> (safeArray.*mfPtr1)(i, (i+1)*10);<br> }<br> <br> for(int i = 0; i < safeArray.length; i++) {<br> cout << (safeArray.*mfPtr2)(i) << " ";<br> }<br> cout << endl;<br> <br> SafeArray *safePtr = &safeArray;<br> <br> (safePtr->*mfPtr1)(2, 100);<br> <br> for(int i = 0; i < safePtr->length; i++) {<br> cout << (safePtr->*mfPtr2)(i) << " ";<br> }<br> cout << endl; <br><br> return 0;<br>}</pre>
<span class="postbody"><br>
执行结果:</span><br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">10 20 30 40 50 60 70 80 90 100<br>
10 20 100 40 50 60 70 80 90 100</span></small><span style="color: rgb(255, 255, 255);"><br>
</span></td>
</tr>
</tbody>
</table>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -