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

📄 subject_48321.htm

📁 vc
💻 HTM
字号:
<p>
序号:48321 发表者:wwww 发表日期:2003-07-30 12:17:23
<br>主题:函数声明int Fun() const 请问const代表什么意思?
<br>内容:函数声明int Fun() const 请问const代表什么意思?
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:Fang 回复日期:2003-07-30 14:06:22
<br>内容:Constant Member Functions<BR>C++ Specific <BR><BR>Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called.<BR><BR>To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition. A constant member function cannot modify any data members or call any member functions that aren't constant.<BR><BR>END C++ Specific<BR><BR>Example<BR><BR>// Example of a constant member function<BR>class Date<BR>{<BR>public:<BR>&nbsp;&nbsp; Date( int mn, int dy, int yr );<BR>&nbsp;&nbsp; int getMonth() const;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // A read-only function<BR>&nbsp;&nbsp; void setMonth( int mn );&nbsp;&nbsp;&nbsp;&nbsp;// A write function;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;cannot be const<BR>private:<BR>&nbsp;&nbsp; int month;<BR>};<BR><BR>int Date::getMonth() const<BR>{<BR>&nbsp;&nbsp; return month;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Doesn't modify anything<BR>}<BR>void Date::setMonth( int mn )<BR>{<BR>&nbsp;&nbsp; month = mn;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Modifies data member<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

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