⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pointertostaticmember.html

📁 关于 C++ 的历史无须我来介绍了
💻 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 static class member)</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:&nbsp;静态类别成员指标(Pointer to static class member)</a></h1>

<a href="MemberFunctionPtr.html">成员函式指标</a>
不可以用来存取静态类成员函式,对于静态类别成员,其指标宣告方式与一般指标宣告方式是类似的,例如有这么一个类别:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">class SomeClass {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">public:</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
static int someVar;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; ....</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
static int someFunction() {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; ....</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp;
&nbsp; &nbsp; return otherVar;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; }</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>

如果想宣告一个可指向someVar的指标,则与一般指标的宣告无异,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int *iPtr =
&amp;SomeClass::someVar;</span><br>

</div>

<br>

当然,您必须注意到存取权限必须是public的成员可以存取,之后取值时与一般指标的取值方法相同:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt; *iPtr
&lt;&lt; endl; // 显示SomeClass::someVar的值</span><br>

</div>

<br>

如果想宣告一个可指向someFunction()的指标,则与函式指标的宣告无异,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int (*fPtr)() =
&amp;SomeClass::someFunction;</span><br>

</div>

<br>

之后呼叫函式与一般函式的呼叫无异,例如:<br>

<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">cout &lt;&lt; fPtr()
&lt;&lt; endl; // 显示SomeClass::someFunction()的传回值</span></div>

<br>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -