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

📄 constandmuttable.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>const 与 mutable</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: const 与 mutable</a></h1>

假设您设计了一个Ball类别:<br>

<ul>

  <li>Ball.h</li>

</ul>

<pre>#include &lt;string&gt;<br>using namespace std;<br><br>class Ball { <br>public: <br>    Ball(); <br>    Ball(double, const char*);     <br>    Ball(double, string&amp;); <br>    <br>    double radius() {<br>        return _radius;<br>    }<br>    <br>    string&amp; 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&amp; 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>

<span class="postbody"><br>

假设您现在设计了一个somefun()函式:<br>

</span>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;" class="postbody">void
somefun(const Ball &amp;ball) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;" class="postbody">&nbsp;&nbsp;&nbsp;&nbsp;
ball.radius();</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

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

<span class="postbody"></span></div>

<span class="postbody"><br>

在函式的参数列上,您使用const宣告了ball参数,在编译时会出现以下的讯息:<br>

</span>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace; color: rgb(255, 0, 0);" class="postbody">passing
`const Ball' as `this' argument of `double Ball::radius()' discards
qualifiers </span><br>

<span class="postbody"></span></div>

<span class="postbody"><br>

由于参数列上ball使用了const来宣告,这表示您不可以更动ball实例的状态,也就是不得(在呼叫函式时)更动ball的资料成员,为了让编译器
得知这项讯息,您要在所呼叫的函式上加上const,例如:<br>

</span>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;" class="postbody">void
radius() const {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

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

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

<span class="postbody"></span></div>

<span class="postbody"><br>

编译器会检查每个被标示为const的成员函式,看看当中的陈述有无更动物件的资料成员。<br>

<br>

另一方面还有一个问题,假设您在somefun()函式中如下呼叫:<br>

</span>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;" class="postbody">void somefun(const Ball &amp;ball) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;" class="postbody">&nbsp;&nbsp;&nbsp;&nbsp;
ball.name();</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;" class="postbody">}</span><br>
</div>



<span class="postbody"><br>

则编译时会出现以下的错误讯息:<br>

</span>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace; color: rgb(255, 0, 0);" class="postbody">`const
Ball' as `this' argument of `std::string&amp; Ball::name()' discards
qualifiers </span><br>

<span class="postbody"></span></div>

<span class="postbody"><br>

即使您在name()函式后加上const,编译时照样无法通过,原因在于name()是以传参考的方式传回,而不是以传值的方式传回,由于以传参考的方
式传回,接受传回值的物件可以直接更改传回值,因而影响被呼叫物件的状态,为了保证传回值不被修改,您要在传回值宣告上加上const,也就是:<br>

</span>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;" class="postbody">const
string&amp; name() const {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

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

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

<span class="postbody"></span></div>

<span class="postbody"><br>

</span>有时候您会希望大部份成员在const成员函式中不被更改,但少数几个资料成员允许它们在const成员函式中被更动,因为这些资料成员被
变动并不被视为改变了物件的状态,这时候您可以在该资料成员宣告时加上mutable,表示对该成员的变动并不代表对物件状态的改变,例如: <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; ....</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;
double increment() const { </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 _index++; </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;} </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;">private:</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;
mutable double _index; </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>

<span class="postbody"></span><br>

</body>
</html>

⌨️ 快捷键说明

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