[m [1;34m※ 來源:‧蛋捲廣場 bbs.tku.edu.tw‧[FROM: 218.162.2.78] [m
意思就是說,可以把 const 的變數傳給它囉?
允許處理,是不是可以取址、取值...
但還是不能改變其內容吧?
--
有時候,人一旦被傷害了,自暴自棄就成了唯一的選項。
人們不明白的是,真正能令自己悲傷的,正是自己最珍貴的東西。
因為珍貴,所以永遠都不能放棄;因為珍貴,所以永遠都不該掉頭就走。
多年以後,我終於領悟這個道理。
--
[1;32m※ Origin: [33m元智大學 風之塔 [37m<bbs.yzu.edu.tw> [m
[1;32m※ From : [36m218-162-11-194.hinet-ip.hinet.net [m
不是這樣的
member function加上const修飾指的是此member function不會也不能更改
member variable (mutable 變數除外) 也就是說const 的member function
不能更改到此物件的state
--
※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw> ◆ From: 211.21.191.127
Constant Member Functions
C++ Specific
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.
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.
END C++ Specific
Example
// Example of a constant member function
class Date
{
public:
Date( int mn, int dy, int yr );
int getMonth() const; // A read-only function
void setMonth( int mn ); // A write function;
// cannot be const
private:
int month;
};
int Date::getMonth() const
{
return month; // Doesn't modify anything
//這裡的Doesn't modify anything是指連常數都不行嗎?
// 請高手解答吧
}
void Date::setMonth( int mn )
{
month = mn; // Modifies data member
hmm....它真的這樣寫?
把這本書丟了吧.....:p
不然賣掉再貼點錢買本好一點的也行~~:p
> 我想應該以msdn為主吧..
其實不應該以 MSDN 為準,M$ 又不算是 C++ Standard 規格的制定機構。
BCB 和 GNU 的 compiler 還比 M$ 的接近標準呢!
花錢可以買到標準的 spec 的。
--
※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw> ◆ From: 61-216-50-128.HINET-IP.hinet.net
跟member function是否是const 一點關係也沒有
--
※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw> ◆ From: g894314.HSIN-B.ab.nthu.edu.tw
沒錯~
這麼爛的書不要也罷~
像c++ 這麼複雜的程式語言, 要能夠很精確描述出他的精神且兼顧到眾多細節
其實沒那麼簡單, 國內的作家寫的c++的書幾乎都不夠格
建議你看c++之父寫的the c++ programming language.
裡頭有一般國產c++教材裡面所看不到的深度與廣度
(有中譯本 , 是william大大翻的 )
> > 我想應該以msdn為主吧..
> 其實不應該以 MSDN 為準,M$ 又不算是 C++ Standard 規格的制定機構。
> BCB 和 GNU 的 compiler 還比 M$ 的接近標準呢!
> 花錢可以買到標準的 spec 的。
comeau的c/c++ compiler比上述的compiler都更接近iso或ansi的標準。
而且比上述產品都便宜(很多)。
msdn本身並沒問題,
尤其在基本的c/c++ syntax description的地方。
我引約記得有人把specification掃成pdf放在網路上。
最後,話說回來,
看specification如果手頭上的compiler不支援不是白搭?
--
[m [1;33m※來源 : [1;36m 台北科大計中紅樓資訊站 [1;35mredbbs.cc.ntut.edu.tw
[1;32m※FROM : [1;37m140.124.13.2 [m
一般不能被修改的變數,不就是叫做常數嗎?:-)
--
※ Origin: 窈窕淑女 ◆ From: 203.204.86.140
--
※【窈窕淑女】telnet://lady.twbbs.org
http://lady.twbbs.org/~bbs/
很恰當啊, const 是constant 的縮寫,
constant 就是常數的意思,
不然要怎麼翻才恰當呢?
這倒也是,不過如果程式碼是要跑來跑去的(ex:自己寫的library)
又用到比較少用的語法,就只能祈禱了~~~
--
※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw> ◆ From: 61-224-65-127.HINET-IP.hinet.net